0

Image Compression: We have 2 different servers on the same network Server A has document folder where all the documents are saved. Server B has the code which tries to access the documents on Server A and then compress it.

This is where I am running into issues. Error at this line: using (WebClient webClient = new WebClient()) System.NullReferenceException: Object reference not set to an instance of an object.

UNC path given for the file to be accessed across servers: \\ServerName\E$\ClientFiles\SHAH\SHAH_103696_20220531160805_2600.jpg

Any suggestions if the path is correct or If I have to change the path? Here is the method:

public void ImageCompressionStream(ShahImages fileImage)
    {          
        try
        {                
            using (WebClient webClient = new WebClient())
            {
                byte[] data = webClient.DownloadData(fileImage.filePath);

                using (MemoryStream mem = new MemoryStream(data))
                {
                    using (var yourImage = Image.FromStream(mem))
                    {
                         yourImage.Save(fileImage.filePath, ImageFormat.Jpeg);
                    }
                }

            }
        }
        catch (Exception ex)
        {
             
        }
    }

enter code here

Tu deschizi eu inchid
  • 4,117
  • 3
  • 13
  • 24
  • UNC path name should be `\\serverName` not `\serverName`. According to [WebClient](https://learn.microsoft.com/en-us/dotnet/api/system.net.webclient?view=net-6.0) documentation _By default, .NET Framework supports URIs that begin with http:, https:, ftp:, and file: scheme identifiers._. If you're not retrieving a file from a web server, have a look at [Accessing a network file share with C#](https://stackoverflow.com/a/18241044/10024425). – Tu deschizi eu inchid Jun 08 '22 at 14:55
  • I tried \\ServerName. I guess that line got missed while copy pasting. I will edit my question to reflect that. Checked my original posting. It has the \\Servername\ in it. But after saving one of the back slash is being removed. – user2147447 Jun 08 '22 at 15:08
  • Use backtick instead of single-quote. – Tu deschizi eu inchid Jun 08 '22 at 15:17
  • backtick did not help. I am getting the same error. Any other way to access the network path other than UNC path? – user2147447 Jun 08 '22 at 15:41
  • 1
    Changing path from \\ServerName\E$\ClientFiles\SHAH\SHAH_103696_20220531160805_2600.jpg to \\\\ServerName\\E$\ClientFiles\\SHAH\\SHAH_103696_20220531160805_2600.jpg helped me get past that error. However, I am getting a "Access to path denied " error now. Will open another therad for it – user2147447 Jun 08 '22 at 20:34
  • I'm not really understanding your post completely. From the lack of response from anyone else, I imagine others aren't either. The following may be helpful: [How do I ask a good question](https://stackoverflow.com/help/how-to-ask). If you're not able to explain it effectively with words, images and/or drawings may be helpful. – Tu deschizi eu inchid Jun 09 '22 at 04:18

0 Answers0