简体   繁体   English

文件下载asp.net

[英]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 . 我正在尝试使用asp.net上的按钮下载文件,但是在我的情况下,按钮为我提供了webform aspx作为下载文件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: 您可以尝试以下ASP.NET/C#代码段:

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: 为了获得根文件夹和完整路径,您可以像在原始解决方案中那样使用Server.MapPath或以下行来获得更好的性能:

// 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. 此解决方案已经在实际的Web应用程序( http://taxiom.com/Manual_Payday.aspx )中进行了测试/实现-请参阅演示页面右上角的“下载”按钮。 Hope this may help. 希望这会有所帮助。

This is the code I use to download file, make sure fuldFilNavn contains the full path of the file: 这是我用来下载文件的代码,请确保fuldFilNavn包含文件的完整路径:

  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! 这将下载文件名中带有Unicode字符的文件!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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