Problem saving uploaded file

Discussion in 'Site Programming, Development and Design' started by mazuma360, Mar 13, 2011.

  1. On a page that I have hosted here I am having problems save an uploaded file.

    C# Code Behind Button
    otected void lnkSubmitPhoto_Click(object sender, EventArgs e)
    {
    string status = "";
    this.Validate("grpUpload");
    if (IsValid)
    {
    HtmlTableRow row = (HtmlTableRow)lvPhotos.FindControl("rowSubmit");
    HtmlTableCell cell = (HtmlTableCell)row.Cells[0];
    Panel pn = (Panel)cell.FindControl("pnPhoto");
    FileUpload myFile = (FileUpload)pn.FindControl("myFile");
    if (myFile.PostedFile != null)
    {
    int megaByte = 1048576; //Bytes
    HttpPostedFile postedFile = myFile.PostedFile;
    int nFileLen = postedFile.ContentLength;
    if (nFileLen <= megaByte)
    {
    byte[] fileData = new byte[nFileLen];
    postedFile.InputStream.Read(fileData, 0, nFileLen);
    string gamesImagePath = Server.MapPath("~/Images/Games/Photos/");
    System.IO.DirectoryInfo gameImageDirectory = new System.IO.DirectoryInfo(gamesImagePath);
    string fileName = mazuma.Library.File.RandomFileName(gamesImagePath, mazuma.Library.File.GetFileNameExtension(myFile.FileName));
    System.IO.FileInfo[] files = gameImageDirectory.GetFiles(mazuma.Library.File.GetFileName(fileName));
    while (files.Count() > 0)
    {
    fileName = mazuma.Library.File.RandomFileName(gamesImagePath, mazuma.Library.File.GetFileNameExtension(myFile.FileName));
    files = gameImageDirectory.GetFiles(mazuma.Library.File.GetFileName(fileName));
    }
    System.IO.FileStream newFile = new System.IO.FileStream(fileName, System.IO.FileMode.Create);
    newFile.Write(fileData, 0, fileData.Length);
    newFile.Close();
    CheckBox chk = (CheckBox)lvPhotos.FindControl("chkLinkAnonymous");
    string userId = Membership.GetUser(HttpContext.Current.User.Identity.Name).ProviderUserKey.ToString();
    if(DbAccess.AddGamePhoto_User(SiteConfig.DbProviderName, SiteConfig.DbConnectionString, gameID, mazuma.Library.File.GetFileName(fileName), userId, chk.Checked))
    status = "Image File Added.";
    else
    status = "Image File Not Added.";
    }
    status = "Image File To Big (File is " + nFileLen + " byte(s), Max allowed is " + megaByte + " byte(s))";
    }
    else
    {
    status = "No Image File Posted";
    }
    }
    else
    status = "Page Invalid";
    BindPictures();
    lblLinkStatus.Text = "";
    lblVideoStatus.Text = "";
    lblPhotoStatus.Text = status;
    lblCommentStatus.Text = "";
    }

    The error message I am get is below, I think it is erroring out at the bold lines, I have no problems on the localhost, I have read that I should be able to up load:
    Exception Details: System.UnauthorizedAccessException: Access to the path 'E:\web\mazuma36\Images\Games\Photos\abc5e47b-a7d0-4133-9600-f26736de2394.jpg' 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 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via <identity impersonate="true"/>, 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.

    Any ideas of the problem?
     
  2. Ray

    Ray

    Last edited by a moderator: Oct 14, 2015
  3. Thank you
     

Share This Page