4

我正在尝试将文件保存到我网站的文件夹中,但我不断收到 UnauthorizedAccessException 错误。

   [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);
        }
    }

这是视图

@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(path);上收到错误。线

我看不出我到底做错了什么。有什么帮助吗?

4

1 回答 1

3

好像是权限问题

更改productImages文件夹的权限,以便 ASP.NET 可以写入该文件夹。

于 2012-08-07T13:53:34.750 回答