2012-08-07 57 views
3

我試圖將文件保存到我的網站中的文件夾,但我不斷收到UnauthorizedAccessException錯誤。mvc保存文件到文件夾中的網站

[HttpPost] 
    public ActionResult Edit(Product product, HttpPostedFileBase image) 
    { 
     var img = Path.GetFileName(image.FileName); 

     if (ModelState.IsValid) 
     { 
      if (image != null && image.ContentLength > 0) 
      { 
       var path = Path.Combine(Server.MapPath("~/Content/productImages/"), 
             System.IO.Path.GetFileName(image.FileName)); 
       image.SaveAs(path); 
       product.ImageName = img; 

      } 

      // save the product 
      repository.SaveProduct(product); 
      // add a message to the viewbag 
      TempData["message"] = string.Format("{0} has been saved", product.Name); 
      // return the user to the list 
      return RedirectToAction("Index"); 
     } 
     else 
     { 
      // there is something wrong with the data values 
      return View(product); 
     } 
    } 

這裏是VIEW

@using (Html.BeginForm("Edit", "Admin", FormMethod.Post, 
    new { enctype = "multipart/form-data" })) { 

    @Html.EditorForModel() 

    <div class="editor-label">Image</div> 
    <div class="editor-field"> 
     @if (Model.ImageName == null) { 
      @:None 
     } else { 
      <img width="150" height="150" 
       src="@Url.Action("GetImage", "Product", new { Model.ProductID })" /> 
     } 
     <div>Upload new image: <input type="file" name="image" id="image"/></div> 
    </div> 

    <input type="submit" value="Save" /> 
    @Html.ActionLink("Cancel and return to List", "Index") 
} 

我收到的image.SaveAs(路徑)的誤差; line

我看不出究竟我做錯了什麼。任何幫助?

+0

檢查您保存的文件夾的IIS用戶權限 – 2012-08-07 13:54:04

回答

2

看起來像一個權限問題

變化productImages文件夾的權限,以便ASP.NET可以寫入這一點。