使用C#在asp.net中下载文件
问题描述:
如何在asp.net中下载文件?
是我上载的内容:
我将文件上载到网站,并将URL保存到这样的数据库中:
How do i download a file in asp.net? here is what i did to upload it: I upload the file into the website and saved the url to it in a database like this:
string CVPath = null;
if (uploadfiles.HasFile)
{
string file = uploadfiles.FileName;
uploadfiles.PostedFile.SaveAs(Server.MapPath(".") + "//CV//" + file);
CVPath = "~//ProfileImages//" + file;
FileName.InnerText = file;
}
else
CVPath = "";
然后将 CVPath保存在数据库中
and then I save the "CVPath" in a database
答
要下载文件,首先需要将所有内容读取为字符串。
To download a file, first you need to read all the contents to a string.
MemoryStream ms = new MemoryStream();
TextWriter tw = new StreamWriter(ms);
tw.WriteLine("YourString");
tw.Flush();
byte[] bytes = ms.ToArray();
ms.Close();
Response.Clear();
Response.ContentType = "application/force-download";
Response.AddHeader("content-disposition", "attachment; filename=file.txt");
Response.BinaryWrite(bytes);
Response.End();