views:

365

answers:

3

I am using ASP.NET to transmit a .jar file. This code works perfectly on IE. However on Firefox the file downloads, corrupt. What is the best way to fix it? Below is the code I am using.

private void TransferFile()
{
    try
    {
        string filePath = Server.MapPath("SomeJarFIle.jar");

        FileInfo file = new FileInfo(filePath);

        if (file.Exists)
        {
            // Clear the content of the response
            //Response.ClearContent();
            Response.Clear();

            // LINE1: Add the file name and attachment, which will force the open/cance/save dialog to show, to the header
            Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);

            // Add the file size into the response header
            Response.AddHeader("Content-Length", file.Length.ToString());

            // Set the ContentType
            Response.ContentType = ReturnExtension(file.Extension.ToLower());

            // Write the file into the response
            //Response.TransmitFile(file.FullName);
            Response.WriteFile(file.FullName);

            // End the response
            Response.End();
        }
        else
        {
            this.Response.Write("Error in finding file.  Please try again.");
            this.Response.Flush();
        }
    }
    catch (Exception ex)
    {
        this.Response.Write(string.Format("Error: {0}", ex.Message));
    }
}



private string ReturnExtension(string fileExtension)
{
    switch (fileExtension)
    {
        case ".htm":
        case ".html":
        case ".log":
            return "text/HTML";
        case ".txt":
            return "text/plain";
        case ".doc":
            return "application/ms-word";
        case ".tiff":
        case ".tif":
            return "image/tiff";
        case ".asf":
            return "video/x-ms-asf";
        case ".avi":
            return "video/avi";
        case ".zip":
            return "application/zip";
        case ".xls":
        case ".csv":
            return "application/vnd.ms-excel";
        case ".gif":
            return "image/gif";
        case ".jpg":
        case "jpeg":
            return "image/jpeg";
        case ".bmp":
            return "image/bmp";
        case ".wav":
            return "audio/wav";
        case ".mp3":
            return "audio/mpeg3";
        case ".mpg":
        case "mpeg":
            return "video/mpeg";
        case ".rtf":
            return "application/rtf";
        case ".asp":
            return "text/asp";
        case ".pdf":
            return "application/pdf";
        case ".fdf":
            return "application/vnd.fdf";
        case ".ppt":
            return "application/mspowerpoint";
        case ".dwg":
            return "image/vnd.dwg";
        case ".msg":
            return "application/msoutlook";
        case ".xml":
        case ".sdxl":
            return "application/xml";
        case ".xdp":
            return "application/vnd.adobe.xdp+xml";
        case ".jar":
            return "application/java-archive";
        default:
            return "application/octet-stream";
    }
}

UPDATE:

I added the type

case ".jar":
    return "application/java-archive";

And that did not fix the problem. If I zipped up the .jar file it was able to be transmitted fine.

I did notice when I tested againist my localhost the file was downloaded with no issues. However when I push it up to the web server is when I get the problem.

+4  A: 

I don't see a case for ".jar" in the ReturnExtension() function (which I think might be better off named "ReturnMimetype"). Could that be the problem, or did you just forget to paste it in?

The mimetype for .jar is supposed to be application/java-archive. Details here: http://en.wikipedia.org/wiki/Jar-file

I think this is the issue. I remember having the same type of problem when I transmitted a .docx file (which is actually a zip file with a different extension, as a .jar files). The download worked fine in IE, but Firefox corrupted it. The solution was to send the correct mimetype.

amdfan
+1  A: 

I only see two things, it doesn't appear you have a mime type for the .jar file extension.

Two I personally use writefile rather than transmit, but I'm not sure of the difference.

Mitchel Sellers
TransmitFile is kinder to the server with larger files: http://www.ddj.com/windows/202804466
Kev
Kev - thank you very much for that link great information!
Mitchel Sellers
+1  A: 

Use Response.WriteFile or Response.BinaryWrite, there are some known strange behaviours with the TransmitFile method.

seanb