从特定文件夹下载上传文件

时间:2015-07-01 17:48:42

标签: asp.net c#-4.0 visual-studio-2012 file-upload entity-framework-4

我想将文件夹中的上传文件下载到我的项目中.. 我的数据库表

我的上传代码

string filename = Path.GetFileName(FileUploadPatDoc.FileName);
string pathName = Path.GetFullPath(FileUploadPatDoc.PostedFile.FileName);
FileUploadPatDoc.SaveAs(Server.MapPath("~/PatientDocuments/") +filename);
fsDocuments.FileName = filename; // fsDocument is Table Object name
fsDocuments.FilePath = pathName;

它很好地工作并将文档存储在" PatientDocuments"我的项目中的文件夹。 现在,当我想使用图像按钮从Gridview下载时,它无法找到它的目的地路径。 这是上传代码

 ImageButton Btn = (ImageButton)sender;
 GridViewRow row = (GridViewRow)Btn.NamingContainer;
 string fileName, filePath;
 Label file = (Label)grdPatientDocument.Rows[row.RowIndex].FindControl("lblFileName");
 Label path = (Label)grdPatientDocument.Rows[row.RowIndex].FindControl("lblFilePath");

        if (file != null)
        {
            fileName = file.Text;
            filePath = path.Text;
            Response.ContentType = filePath;
            Response.AddHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
            Response.TransmitFile(Server.MapPath(fileName));
            Response.End();   
        }

它会产生以下错误

  

"找不到文件E:\ CoreZ   IT \六月\医院\ 22-06 \最终\ CZ_BHC \ CZ_BHC \ BHC \ CV_MD.Golam_Shahriar.pdf'"

但我需要从E:....\PatientDocuments\CV_MD.Golam_Shahriar.pdf

中找到此文件

请帮助我,我正在使用VS 2012 ...谢谢

1 个答案:

答案 0 :(得分:1)

试试这个

Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName + "");
Response.TransmitFile(filePath);
Response.End();