尝试在远程服务器上上传和保存文件上传时出现异常,但本地框没问题

发布于 2024-08-17 13:18:14 字数 1700 浏览 3 评论 0原文

我有一个 ASP.NET MVC 应用程序,它有一个上传文件的表单。在我的本地机器上,当然它工作正常,但是当我在远程网络服务器上运行它时,我收到此错误:

异常详细信息:System.UnauthorizedAccessException:访问路径“C:\directory\mytestfile.csv”被拒绝.

ASP.NET 无权访问所请求的资源。考虑向 ASP.NET 请求标识授予对资源的访问权限。 ASP.NET 有一个基本进程标识(在 IIS 5 上通常为 {MACHINE}\ASPNET,在 IIS 6 上为 Network Service),如果应用程序不进行模拟,则使用该标识。如果应用程序通过 进行模拟,则身份将是匿名用户(通常为 IUSR_MACHINENAME)或经过身份验证的请求用户。

要授予 ASP.NET 对文件的访问权限,请在资源管理器中右键单击该文件,然后选择“属性”并选择安全选项卡。单击“添加”以添加适当的用户或组。突出显示 ASP.NET 帐户,然后选中所需访问权限的复选框。

源错误: 执行当前 Web 请求期间生成未处理的异常。有关异常来源和位置的信息可以使用下面的异常堆栈跟踪来识别。

我的问题是我事先不知道要上传的文件是什么(因此上传中的文件选择器屏幕),所以我无法预先授权。

这是我的代码:

控制器操作:

 public ActionResult UploadSpreadsheet(HttpPostedFileBase file)
    {

        var fileName = Path.Combine(Request.MapPath("~/App_Data"), Path.GetFileName(file.FileName));
        file.SaveAs(fileName);  //This is what blows up . . .

        using (CsvReader csv = new CsvReader(new StreamReader(fileName_), true))
        {
            string[] headers = csv.GetFieldHeaders();
          . . . .

查看表单:

<form action="/Resources/UploadSpreadsheet" method="post" id="spreadsheetForm" enctype="multipart/form-data">
<fieldset class=outerFieldSet>
<div class="legendTitle">Sync with Spreadsheet</div><hr /><br /><br />

    <label>Choose Spreadsheet: </label><input size="88" class="required" type="file" name="file" />
</fieldset>

<input type="submit" class=longButton value="Syncronize Resource Data" />
</form>

i have an asp.net mvc app that has a form that uploads a file. On my local box, of course it works fine but when i run it on a remote webserver i get this error:

Exception Details: System.UnauthorizedAccessException: Access to the path 'C:\directory\mytestfile.csv' is denied.

ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6) that is used if the application is not impersonating. If the application is impersonating via , the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user.

To grant ASP.NET access to a file, right-click the file in Explorer, choose "Properties" and select the Security tab. Click "Add" to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.

Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

my issue is that i dont know in advance what the file that is going to be upload is (thus the file chooser in the upload screen) so i can't pre authorize it.

here is my code:

Controller Action:

 public ActionResult UploadSpreadsheet(HttpPostedFileBase file)
    {

        var fileName = Path.Combine(Request.MapPath("~/App_Data"), Path.GetFileName(file.FileName));
        file.SaveAs(fileName);  //This is what blows up . . .

        using (CsvReader csv = new CsvReader(new StreamReader(fileName_), true))
        {
            string[] headers = csv.GetFieldHeaders();
          . . . .

View Form:

<form action="/Resources/UploadSpreadsheet" method="post" id="spreadsheetForm" enctype="multipart/form-data">
<fieldset class=outerFieldSet>
<div class="legendTitle">Sync with Spreadsheet</div><hr /><br /><br />

    <label>Choose Spreadsheet: </label><input size="88" class="required" type="file" name="file" />
</fieldset>

<input type="submit" class=longButton value="Syncronize Resource Data" />
</form>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深府石板幽径 2024-08-24 13:18:14

授予 ASP.NET 进程对您要将文件上传到的文件夹的写入权限。请遵循错误消息中概述的相同准则来完成此操作,仅在文件夹级别执行操作。

Grant the ASP.NET process write access to the folder you will be uploading files to. Follow the same guidelines outlined in the error message to accomplish this, only perform the operations at the folder level.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文