0

大家好,我正在上传用户在此路径中上传的文件

       string savefilename = Path.Combine(Server.MapPath("~/Content/UploadedFiles/"),
                                Path.GetFileName());

我将 URL 保存在数据库中的 Url 列中

        ~/Content/UploadedFiles/BugTrackerDataBase.xlsx

我正在尝试通过我的网格视图中的链接检索用户上传的文件我的检索方法如下所示

       public ActionResult ViewAttachments(string AttachmentName)
           {
          try
           {
            AttachmentName = Session["AttachmentUrl"].ToString();
            var fs = System.IO.File.OpenRead(Server.MapPath("'" + AttachmentName + "'"));
            return File(fs, "application/doc", AttachmentName);
        }
        catch
        {
            throw new HttpException(404, "Couldn't find " + AttachmentName);
        }
    } 

我有例外

      "Could not find a part of the path 'D:\AnilWork\BugTracker\BugTracker\ViewBug\'UploadedFiles\BugTrackerDataBase.xlsx''."

谁能告诉我我在哪里做错了或者写程序来做这个

4

2 回答 2

2

那是因为你的路径中有“'”。

\BugTracker\ViewBug\'UploadedFiles\BugTrackerDataBase.xlsx''

删除它们应该可以工作。像这样

var fs = System.IO.File.OpenRead(Server.MapPath(AttachmentName));
于 2012-08-03T09:29:57.563 回答
1

尝试

var fs = System.IO.File.OpenRead(Server.MapPath(" + AttachmentName + "));

代替

var fs = System.IO.File.OpenRead(Server.MapPath("'" + AttachmentName + "'"));

它应该替换为 (Server.MapPath(""+ AttachmentName + ""))

于 2012-08-03T09:31:51.513 回答