允许用户从我的网站通过Response.WriteFile下载() [英] Allowing user to download from my site through Response.WriteFile()

查看:100
本文介绍了允许用户从我的网站通过Response.WriteFile下载()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过点击一个链接,我的网站上以编程方式下载一个文件(这是一个.doc文件坐在我的Web服务器上)。这是我的code:

I am trying to programatically download a file through clicking a link, on my site (it's a .doc file sitting on my web server). This is my code:

string File = Server.MapPath(@"filename.doc");
string FileName = "filename.doc";

if (System.IO.File.Exists(FileName))
{

    FileInfo fileInfo = new FileInfo(File);
    long Length = fileInfo.Length;


    Response.ContentType = "Application/msword";
    Response.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name);
    Response.AddHeader("Content-Length", Length.ToString());
    Response.WriteFile(fileInfo.FullName);
}

这是一个ButtonClick事件处理程序。好吧,我可以做一些有关该文件的路径/文件名code,使其整洁,但点击该按钮时,页面刷新。在本地主机上,这code工作正常,让我可以下载该文件确定。我在做什么错了?

This is in a buttonclick event handler. Ok I could do something about the file path/file name code to make it neater, but when clicking the button, the page refreshes. On localhost, this code works fine and allows me to download the file ok. What am I doing wrong?

感谢

推荐答案

尝试一个稍微修改后的版本:

Try a slightly modified version:

string File = Server.MapPath(@"filename.doc");
string FileName = "filename.doc";

if (System.IO.File.Exists(FileName))
{

    FileInfo fileInfo = new FileInfo(File);


    Response.Clear();
    Response.ContentType = "Application/msword";
    Response.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name);
    Response.WriteFile(fileInfo.FullName);
    Response.End();
}

这篇关于允许用户从我的网站通过Response.WriteFile下载()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆