网页上下载文件提示框(vb.net)
Public Sub downLoadFile(ByVal fPath As String) Dim fileInfo As System.IO.FileInfo = New System.IO.FileInfo(fPath) Response.Clear() Response.AddHeader("content-disposition", "attachment;filename=" + Server.UrlEncode(fileInfo.Name.ToString())) Response.AddHeader("content-length", fileInfo.Length.ToString()) Response.ContentType = "application/octet-stream" Response.ContentEncoding = System.Text.Encoding.Default Response.WriteFile(fPath) End Sub
public async Task<FileStreamResult> DownFile(string fileName) { var url = "http://boldseas.com/image/pro/"+ fileName; var http = new HttpClient(); Stream x = await http.GetStreamAsync(url); return File(x, "application/octet-stream", Server.UrlEncode(fileName)); }
方式一:
public FileStreamResult DownFile(string filePath, string fileName)
{
string absoluFilePath = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["AttachmentPath"] + filePath);
return File(new FileStream(absoluFilePath, FileMode.Open), "application/octet-stream", Server.UrlEncode(fileName));
}
方式二:
public ActionResult DownFile(string filePath, string fileName)
{
filePath = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["AttachmentPath"] + filePath);
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fileName));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
return new EmptyResult();
}
View调用:
<a href="/Document/DownFile?filePath=@item.Value&fileName=@item.Key">下载</a>
其实主要是让浏览器直接下载文件而不是打开文件。
要做两步。
一就是把响应头的类型设置成application/octet-stream
。
二就是设置HTTP响应头的名字为Content-Disposition
,设定值为attachment; filename=theFilename。这里的theFileName就是文件下载对话框里面的默认文件名。
Response.ClearContent(); Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("qrcode.png", System.Text.Encoding.UTF8)); Response.BinaryWrite(ms.ToArray());
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· 程序员常用高效实用工具推荐,办公效率提升利器!
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 【译】WinForms:分析一下(我用 Visual Basic 写的)