【发布时间】:2019-08-21 09:57:34
【问题描述】:
在我的 ASP.NET Web 应用程序中,我尝试使用 JQuery Ajax 上传文件,该文件又调用 .ashx handler。文件应保存到在项目树下创建的pdf 文件夹,使用:
添加 --> 新文件夹
部署和测试时,SaveAs 失败并出现access is denied 错误。
互联网上的所有解决方案都建议对文件夹进行编辑权限。所以在服务器上,我授予Network Service 用户对pdf 文件夹的完全控制权,然后上传工作...但问题是Web 应用程序的任何后续部署只会删除那些服务器上的权限,当然问题又回来了。
我将Network Service 对Inetpub 文件夹的完全控制权交给了Inetpub 文件夹以解决该问题,从而解决了此案。但是这样做可以接受吗?
感觉不对,请问您建议什么来解决问题而不是我的解决方法?
处理程序中的代码如下:
public void ProcessRequest(HttpContext context)
{
try
{
if (context.Request.Files.Count > 0)
{
HttpPostedFile postedFile = context.Request.Files[0];
string filePath = context.Server.MapPath("~/pdf");
string fileName = Path.GetFileName(postedFile.FileName);
postedFile.SaveAs( Path.Combine(filePath , fileName) );
string json = new JavaScriptSerializer().Serialize(
new { name = fileName });
context.Response.StatusCode = (int)HttpStatusCode.OK;
context.Response.ContentType = "text/json";
context.Response.Write(json);
context.Response.End();
}
}
catch (Exception e)
{
context.Response.ContentType = "text";
context.Response.Write(e.Message);
context.Response.End();
}
}
提前致谢
【问题讨论】:
标签: asp.net access-denied httppostedfile