如何在asp.net C#网站下载 [英] how to download in asp.net C# website
本文介绍了如何在asp.net C#网站下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在asp.net C#网站下载?请输入代码和命名空间。
how to download in asp.net C# website? please type with code and namespace.
推荐答案
这个问题毫无意义。您不需要代码来提供从网站下载,您只需要一个指向该文件的链接。您可以编写将文件写入Response对象的代码,如果需要,可以在您的书籍或在线中轻松找到该代码。
This question makes no sense. You don''t need code to offer a download from a website, you just need a link to the file. You can write code that writes a file to the Response object, that code is easy to find in your books or online if you need it.
public static string ServerMapPath(string path)
{
return HttpContext.Current.Server.MapPath(path);
}
public static HttpResponse GetHttpResponse()
{
return HttpContext.Current.Response;
}
public static void DownLoadFileFromServer(string fileName)
{
//This is used to get Project Location.
try
{
string filePath = ServerMapPath(fileName);
//This is used to get the current response.
HttpResponse res = GetHttpResponse();
res.Clear();
res.AppendHeader("content-disposition", "attachment; filename=" + filePath);
res.ContentType = "application/octet-stream";
res.WriteFile(filePath);
res.Flush();
res.End();
}
catch (Exception ex)
{
}
}
protected void Button2_Click(object sender, EventArgs e)
{
DownLoadFileFromServer("~/Storage/" + txtname.Text);
}
文件需要在某个文件夹的同一个网站目录中int这个案例文件夹名称是存储
file need to be in same directory of website in some folder int this case folder name is Storage
这篇关于如何在asp.net C#网站下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文