如何通过asp.net c#编码下载docx / pdf文件? [英] how to download docx/pdf file through asp.net c # coding?

查看:70
本文介绍了如何通过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.


这篇关于如何通过asp.net c#编码下载docx / pdf文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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