如何通过asp.net c#编码下载docx / pdf文件?

如何通过asp.net c#编码下载docx / pdf文件?

问题描述:

无法下载docx文件和pdf文件

这里是我的代码..



not able to download docx file and pdf file
here is my code..

var myMemoryStream = new MemoryStream();
Response.Clear();
 Response.ContentType = "Application/msword";
 Response.AddHeader("Content-Disposition";"attachment;filename=" + strFilename);
 Response.BinaryWrite(myMemoryStream.ToArray());
 Response.Flush();
           
 Response.Close();
 Response.End();

好吧,不 - 它不会。

查看你的代码:

Well, no - it won't.
Look at your code:
var myMemoryStream = new MemoryStream();
Response.Clear();
 Response.ContentType = "Application/msword";
 Response.AddHeader("Content-Disposition";"attachment;filename=" + strFilename);
 Response.BinaryWrite(myMemoryStream.ToArray());



myMemoryStream是 new ,所以包含no数据 - 所以系统(正确)不传输任何字节...这意味着没有下载。



也许你忘了加载 MemoryStream 包含某个文件或数据库中的数据?


myMemoryStream is new, so contains no data - so the system (correctly) transfers no bytes...and that means no download.

Perhaps you forgot to load the MemoryStream with data from a file or database somewhere?


string filename = Server.MapPath(〜/ File / DocFile /)+ strFilename;

Response.Redirect(〜/ File / DocFile /+ strFilename);





这两行足以下载来自您所在位置的文件。



感谢

OriginalGriff对此主题有所了解。
string filename = Server.MapPath("~/File/DocFile/") + strFilename;
Response.Redirect("~/File/DocFile/" + strFilename);


these two lines r enough to download a file from your location.

thanks to
OriginalGriff gives some light on this topic.