Reputation: 2293
I'm trying to save files to a folder in my site, but I keep receiving an UnauthorizedAccessException error.
[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);
}
}
HERE IS THE 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")
}
I'm receiving the error on the image.SaveAs(path); line
I can't see what exactly I'm doing wrong. Any help?
Upvotes: 5
Views: 32767
Reputation: 218732
Looks like a permission problem
Change the Permissions on productImages
folder so that ASP.NET can write to that.
Upvotes: 3