c# .net Adsense ADO.NET Linq Viruses/security asp.net MVC JQuery Angular-js Node-js SEO Java C++ SQL API Networking vb.net .Net Css JavaScript Generics c#.Net entity framework HTML Website host Website Construction Guide HTTP tutorial W3C tutorial Web Services JSON Psychology Ionic framework Angular ReactJS Python Computer Android
c# .net

Could not find file 'C:\Program Files\Common Files\Microsoft Shared\DevServer\11.0\photo.jpg'

| | ASP-NET , CSharp

I got the following error while uploading a file using FileUpload control. I solved by getting the file contents in bytes so you can assign to it.

  public void Compress()
    {
        string fileWithExt = Path.GetFileName(FileUpload1.FileName);
        string zipFile = fileWithExt + ".zip";
        string zipFilePath =  Path.Combine(Server.MapPath("~/Uploads"), zipFile);
        using (FileStream target = new FileStream(zipFilePath, FileMode.Create, FileAccess.Write))
        using (GZipStream zipstream = new GZipStream(target, CompressionMode.Compress))
        {
            byte[] data = File.ReadAllBytes(Path.GetFileName(FileUpload1.FileName));
           zipstream.Write(data, 0, data.Length);
           zipstream.Flush();
        }
    }

Exception message:

 

Solution:

 You just replace the above code with the highlighted code.

byte[] fileBytes = FileUpload1.FileBytes;
      using (FileStream target = new FileStream(zipFilePath, FileMode.Create, FileAccess.Write))
       using (GZipStream zipstream = new GZipStream(target, CompressionMode.Compress))
        {
           byte[] data = fileBytes;
           zipstream.Write(data, 0, data.Length);
           zipstream.Flush();       }