C# Response Shows Download Unsuccessful Error on A

2019-08-14 14:35发布

问题:

I used Asp.net and C# to run this website and to approach mobile version, I used jQuery mobile. When I download via desktop browser is working fine and when I try to download via mobile, I got error

Download Unsuccessful

How can I fix it?

Here is my code

protected void lbAttach_Click(object sender, EventArgs e)
{
    if (this.IsPostBack)
    {                
        string sRFP = lblRFPNo.Text.ToString().Trim();
        if (dsRFP(sRFP).Tables[0].Rows[0]["Attachment"].ToString() != "")
        {
            byte[] Attach = (byte[])dsRFP(sRFP).Tables[0].Rows[0]["Attachment"];
            string strfn = Convert.ToString(dsRFP(sRFP).Tables[0].Rows[0]["AttachName"]);
            string ext = Path.GetExtension(strfn);

            Response.Buffer = true;
            Response.Clear();
            Response.ClearContent();
            Response.ClearHeaders();
            Response.AddHeader("Content-Disposition", "attachment; filename=\"" + strfn.ToUpper() + "\"" );

            //BinaryWriter bw = new BinaryWriter(Response.OutputStream);
            //bw.Write(Attach);
            //bw.Close();
            Response.ContentType = ReturnExtension(ext);

            try
            {
                Response.BinaryWrite(Attach);
            }
            catch (Exception ex)
            {
                lblMessage.Text = ex.Message;
            }

            Response.End();
            Response.Flush();
            Response.Close();
        }
    }
}

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";
        default:
            return "application/octet-stream";
    }
}

回答1:

I had similar issue, file download was failing in Chrome. I just removed Response.Flush() and Response.Close() and added just Response.End() which worked