views:

138

answers:

1

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>
+2  A: 

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.

JonathanK
thanks this worked but there is still an issue. the issue is that since this is a directory under my deployed build, everytime a do a new release i have to readd this permission. is there anyway around this?
ooo
Once you have the permissions setup, omit the directory from your deployment to avoid overwriting it on the server.
JonathanK
but the issue is that i deploy my app to C:\directory and this is C:\directory\app_data so i am wiping out C:\directory each time i deploy. maybe i am deploying too much or shouldn't do a delete first but i thought that was cleaner
ooo
I see. Yes, either do not delete C:\directory when you are doing your deploy or as an alternative, create a different folder on the server with the required permissions and store your uploaded files there. The latter is a cleaner overall solution anyway.
JonathanK
yup . . worked . . i get my initial deploy scripts were a little to brute force . . i updated the scripts to just deploy to the specific directories that i need and i leave app_data alone . .thanks for the help
ooo