简体   繁体   中英

FIle Downloading asp.net

I am trying to download a file using a button on asp.net, but the button gives me the webform aspx as the the downloaded file in my case DownloadFileTest.apsx . I needed to download the right file. This might help I the uploaded file in my solution explorer doesn't show up either. But it shows up if I access it inside the folder of my project. Here is the code

 protected void Button1_Click(object sender, EventArgs e)
    {
        string filename = TextBox1.Text;
        Response.ContentType = "application/octet-stream";
        Response.AppendHeader("content-diposition", "attach;filename" + filename);
        Response.TransmitFile(Server.MapPath("~/CustomerFiles/" + filename));
        Response.End();
    }

You may try the following ASP.NET/C# code snippet:

internal static void Download(string FileName)
{
    HttpResponse _response = HttpContext.Current.Response;
    FileStream _fileStream;
    byte[] _arrContentBytes;
    try
    {
        // clear response obj
        _response.Clear();

        // clear content of response obj
        _response.ClearContent();

        // clear response headers
        _response.ClearHeaders();

        // enable response buffer
        _response.Buffer = true;

        // specify response content
        _response.ContentType = ContentType;

        _response.StatusCode = 206;
        _response.StatusDescription = "Partial Content";

        // create FileStream: IMPORTANT - specify FileAccess.Read
        _fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read);

        // Bytes array size= (int)_fs.Length;
        _arrContentBytes = new byte[(int)_fileStream.Length];

        // read file into bytes array
        _fileStream.Read(_arrContentBytes, 0, (int)_fileStream.Length);

        // add response header
        _response.AddHeader("content-disposition", "attachment;filename=" + FileName);

        // ACTUAL PROCEDURE: use BinaryWrite to download file
        _response.BinaryWrite(_arrContentBytes);

        // ALTERNATIVE: TransmitFile
        //_response.TransmitFile(filePath);

        // close FileStream
        _fileStream.Flush();
        _fileStream.Close();

        _response.Flush();
        HttpContext.Current.ApplicationInstance.CompleteRequest();
    }
    catch { }
    finally
    {
        _fileStream = null;
        _arrContentBytes = null;
    }
}

In order to get the root folder and full path you may use Server.MapPath as in your original solution or the following line for better performance:

// get the root dir; fast
string _root = AppDomain.CurrentDomain.BaseDirectory;

This solution has been tested/implemented in the actual web app ( http://taxiom.com/Manual_Payday.aspx ) - refer to the "Download" button in the upper-right corner of the page for the demo. Hope this may help.

This is the code I use to download file, make sure fuldFilNavn contains the full path of the file:

  public static void DownloadFil(string fuldFilNavn) { HttpContext context = HttpContext.Current; context.Response.ClearHeaders(); context.Response.ClearContent(); string filNavn = Uri.EscapeDataString(Path.GetFileName(fuldFilNavn)).Replace("+", "%20"); context.Response.AppendHeader("Content-Disposition", "attachment;filename*=utf-8''" + filNavn); context.Response.AppendHeader("Last-Modified", File.GetLastWriteTimeUtc(fuldFilNavn).ToString("R")); context.Response.ContentType = "application/octet-stream"; context.Response.AppendHeader("Content-Length", new FileInfo(fuldFilNavn).Length.ToString()); context.Response.TransmitFile(fuldFilNavn); context.Response.End(); } 

This will download files with unicode characters in the filename!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM