拒绝访问路径Server.MapPath [英] Access to the path Server.MapPath is denied

查看:97
本文介绍了拒绝访问路径Server.MapPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个pdf文档

I created one pdf document

        var document = new Document();
        string path = Server.MapPath("AttachementToMail");
        PdfWriter.GetInstance(document, new FileStream(path + 
                  "/"+DateTime.Now.ToShortDateString()+".pdf", FileMode.Create));

现在我要下载此文档

 Response.ContentType = "Application/pdf";
 Response.AppendHeader("Content-Disposition", "attachment; filename="+   
                                DateTime.Now.ToShortDateString() + ".pdf" + "");
 Response.TransmitFile(path);
 Response.End();

但是它给了我错误拒绝访问路径〜\ AttachementToMail".

存在对IIS_IUSRS的读/写访问权限

read / write access for IIS_IUSRS exists

推荐答案

您提供的要写入的路径是虚拟路径. TransmitFile 需要绝对路径.

The path you are providing to write is a virtual path. TransmitFile expects an absolute path.

您的代码应如下所示:

var document = new Document();
string path = Server.MapPath("AttachementToMail");
var fileName =  DateTime.Now.ToString("yyyyMMdd")+".pdf";
var fullPath = path + "\\" + fileName;

//Write it to disk
PdfWriter.GetInstance(document, new FileStream(fullPath, FileMode.Create));

//Send it to output
Response.ContentType = "Application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename="+ fileName );

Response.TransmitFile(fullPath);
Response.Flush();
Response.End();

DateTime.Now 表示当前时间.在使用它作为文件名时要小心.使用 ToShortDateString 有点冒险,因为某些文化将/设置为该格式.使用 ToString 将允许您固定文件名格式,而不考虑服务器的区域性.

DateTime.Now represents the current time. Be careful when you use it as the file name. Using ToShortDateString is a little risky, as some cultures put / in that format. Using ToString will allow you to fix your filename format regardless of the server culture.

这篇关于拒绝访问路径Server.MapPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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