?
Solved

FireFox cannot download zip file from ASP.NET

Posted on 2009-12-29
14
Medium Priority
?
631 Views
Last Modified: 2013-12-17
Hi Experts,

I am using ASP.NET Response.Write to transfer a file to the browser. In IE, whatever the ContentType I set in Response, it works fine. However in Firefox, I got following error (see the screenshot). I understand that the firefox is following the HTTP standard that I have to specfy the content type. Actually I already do. The zip file cannot be recognized by firefox but the other file types work fine (xls, doc, ppt). Does anybody know what happened and how to solve the issue?

This is what I get from the Internet:
"Any HTTP/1.1 message containing an entity-body SHOULD include a Content-Type header field defining the media type of that body. If and only if the media type is not given by a Content-Type field, the recipient MAY attempt to guess the media type via inspection of its content and/or the name extension(s) of the URI used to identify the resource."

I am using firefox 3.5.6. Any help will be appreciated. Thanks in advance!
void foo()
{
                 //Zip all the files
                 Response.ContentType = "application/x-zip-compressed";
                 string filepath = AxImageFileCacheMgr.CreateZipPackage(sess, "ProtectedFiles.Zip", _docCtx.IRMRenderPaths);
                 using (System.IO.Stream outStream = XtenderSolutions.Utility.IO.File.Open(filepath, Global.Config, sess.Credentials))
                 {
                    Response.Charset = "65001";
                    string downloadFileName = HttpUtility.UrlEncode(String.Format("{0}.zip", docIndexes[0]));
                    Response.AddHeader("Content-Disposition", "attachment;Filename=\"" + downloadFileName + "\"");
                    WriteStream(outStream, true);
                 }
}            

      public static string CreateZipPackage(CMSession sess, string mainFileName, string[] fileNames)
      {
          string packagePath = null;
          WindowsImpersonationContext wic = null;
          TempFileCache tempCache = new TempFileCache();
          UncConfig cachePath = TempFileCache.CachePath;
          wic = cachePath.Authentication.Impersonate(sess.Credentials);
          string zipfileName = mainFileName + ".ZIPpackage";
          packagePath = tempCache.FileFullName(zipfileName);
          using (Package package = Package.Open(packagePath, FileMode.Create))
          {
              foreach (string file in fileNames)
              {
                  string fileName = System.IO.Path.GetFileName(file);
                  string extension = System.IO.Path.GetExtension(file);
                  Uri partUri = PackUriHelper.CreatePartUri(new Uri(fileName, UriKind.Relative));
                  // Add the file as a part to the Package
                  PackagePart packageFile = package.CreatePart(partUri, GetMimeString(extension));
                  // Copy the data to the Document Part
                  using (FileStream inputFileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
                  {
                      WriteStream(inputFileStream, packageFile.GetStream());
                  }
              }
          }
		  return packagePath;
      }

Open in new window

ScreenshotFireFox.JPG
0
Comment
Question by:magicdlf
  • 6
  • 6
  • 2
14 Comments
 
LVL 11

Expert Comment

by:ICaldwell
ID: 26143479
change this part of your code
void foo()
{
                 //Zip all the files
                 string filepath = AxImageFileCacheMgr.CreateZipPackage(sess, "ProtectedFiles.Zip", _docCtx.IRMRenderPaths);
                 Response.AddHeader("Content-Disposition", "attachment;Filename=" + downloadFileName);
                 Response.ContentType = "application/octet-stream";
                 Response.Flush();
                 using (System.IO.Stream outStream = XtenderSolutions.Utility.IO.File.Open(filepath, Global.Config, sess.Credentials))
                 {
                    string downloadFileName = HttpUtility.UrlEncode(String.Format("{0}.zip", docIndexes[0]));
                    WriteStream(outStream, true);
                 }
                 Response.End;
}  

Open in new window

0
 
LVL 11

Expert Comment

by:ICaldwell
ID: 26143495
oops, also add the Response.Charset
void foo()
{
                 //Zip all the files
                 string filepath = AxImageFileCacheMgr.CreateZipPackage(sess, "ProtectedFiles.Zip", _docCtx.IRMRenderPaths);
                 Response.AddHeader("Content-Disposition", "attachment;Filename=" + downloadFileName);
                 Response.ContentType = "application/octet-stream";
                 Response.Charset = "65001";
                 Response.Flush();
                 using (System.IO.Stream outStream = XtenderSolutions.Utility.IO.File.Open(filepath, Global.Config, sess.Credentials))
                 {
                    string downloadFileName = HttpUtility.UrlEncode(String.Format("{0}.zip", docIndexes[0]));
                    WriteStream(outStream, true);
                 }
                 Response.End;
}  

Open in new window

0
 
LVL 9

Author Comment

by:magicdlf
ID: 26143501
Hi ICaldwell,
Thanks for your advice, actually the octet-stream is the first thing I have tried. I am trying your code now, any other idea?

Btw, here's my response header (I changed the content type to application/binary, still not working):
HTTP/1.1 200 OK
Server: Microsoft-IIS/5.1
Date: Wed, 30 Dec 2009 05:36:01 GMT
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Content-Disposition: attachment;Filename="PDF.zip"
Cache-Control: private
Content-Type: application/binary; charset=UTF-8
Content-Length: 434594
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 9

Author Comment

by:magicdlf
ID: 26143510
In the WriteStream, the response header will be flushed at the first time.  I don't need to worry about that.
0
 
LVL 13

Expert Comment

by:zadeveloper
ID: 26143776
Tested working in Firefox  3.5.6
 

void DownloadZipFile(string zipData, string fileName) 
{ 
Response.ContentType = "application/zip"; 
Response.AddHeader("Content-Disposition", "attachment;Filename=\"" + fileName + "\""); 
Response.Clear(); 
Response.ContentType = "application/zip"; 
Response.AddHeader("Content-Disposition", "inline; filename=" + fileName); 
var streamData = Encoding.ASCII.GetBytes(zipData); 
Response.OutputStream.Write(streamData, 0, streamData.Length); 
Response.End(); 
}

Open in new window

0
 
LVL 13

Expert Comment

by:zadeveloper
ID: 26143779
sorry - left some test stuff in there, lets try this again :)
private void DownloadZipFile(string zipData, string fileName)
    {
        Response.Clear();
        Response.ContentType = "application/zip";
        Response.AddHeader("Content-Disposition", "inline; filename=" + fileName);

        var streamData = Encoding.ASCII.GetBytes(zipData);

        Response.OutputStream.Write(streamData, 0, streamData.Length);
        Response.End();
    }

Open in new window

0
 
LVL 13

Expert Comment

by:zadeveloper
ID: 26174120
any solution on this question ?
0
 
LVL 13

Expert Comment

by:zadeveloper
ID: 26206490
Did this work for you ?
0
 
LVL 9

Author Comment

by:magicdlf
ID: 26206500
Sorry, your code is just a sample but I am still stucking with the real problem. I am trying to find out the solution. Once I get it I will post it here and let you know. Thanks for replying.
0
 
LVL 13

Expert Comment

by:zadeveloper
ID: 26206518
np.

Buti did test my code on firefox and it downloads the files perfectly.
0
 
LVL 9

Author Comment

by:magicdlf
ID: 26207491
Here's an update. It ONLY happens when I tried to download a zip from modaldialog. I think you will be easily to reproduce this. Any idea?
I use this to open a modal dialog in firefox 3.5.6. The window should be modal. I don't know if there is any alternatives.
    window.showModalDialog(url, window, "dialogWidth:500px;dialogHeight:550px;resizable:no;scroll:yes;status:yes;title:no;menu:no;toolbar:no");
0
 
LVL 9

Accepted Solution

by:
magicdlf earned 0 total points
ID: 26207610
0
 
LVL 13

Expert Comment

by:zadeveloper
ID: 26290751
Hi magicdlf:
Any chance you could post the solution here so we know what the issue was?
0
 
LVL 9

Author Comment

by:magicdlf
ID: 26290758
I already posted. See me last posts.
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question