c# – 从asp.net中的文件夹下载文件
发布时间:2020-12-16 02:00:42 所属栏目:百科 来源:网络整理
导读:我正在开发一个网站,我上传文件并存储在文件夹中.上传文档工作正常,但下载代码不起作用.我需要从文件夹下载文件. protected void btnDownload_Click(object sender,EventArgs e){ lblresume.Text = "~/Student_Resume/" + fuResume.FileName.ToString(); if
我正在开发一个网站,我上传文件并存储在文件夹中.上传文档工作正常,但下载代码不起作用.我需要从文件夹下载文件.
protected void btnDownload_Click(object sender,EventArgs e) { lblresume.Text = "~/Student_Resume/" + fuResume.FileName.ToString(); if (lblresume.Text != string.Empty) { string filePath = lblresume.Text; Response.ContentType = "doc/docx"; Response.AddHeader("Content-Disposition","attachment;filename="" + filePath + """); Response.TransmitFile(Server.MapPath(filePath)); Response.End(); } } 解决方法
试试这个
protected void btnDownload_Click(object sender,EventArgs e) { lblresume.Text = "~/Student_Resume/" + fuResume.FileName.ToString(); if (lblresume.Text != string.Empty) { WebClient req = new WebClient(); HttpResponse response = HttpContext.Current.Response; string filePath = lblresume.Text; response.Clear(); response.ClearContent(); response.ClearHeaders(); response.Buffer = true; response.AddHeader("Content-Disposition","attachment;filename=Filename.extension"); byte[] data = req.DownloadData(Server.MapPath(filePath)); response.BinaryWrite(data); response.End(); } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |