繁体   English   中英

文件下载asp.net

[英]FIle Downloading asp.net

我正在尝试使用asp.net上的按钮下载文件,但是在我的情况下,按钮为我提供了webform aspx作为下载文件DownloadFileTest.apsx 我需要下载正确的文件。 这可能有助于我在解决方案资源管理器中上传的文件也不会显示。 但是如果我在项目文件夹中访问它,它就会显示出来。 这是代码

 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();
    }

您可以尝试以下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;
    }
}

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

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

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

这是我用来下载文件的代码,请确保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(); } 

这将下载文件名中带有Unicode字符的文件!

暂无
暂无

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

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