【问题标题】:mvc save file to folder in sitemvc 将文件保存到站点中的文件夹
【发布时间】:2012-08-04 13:23:42
【问题描述】:

我正在尝试将文件保存到我网站的文件夹中,但我不断收到 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); 行收到错误

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

【问题讨论】:

  • 检查您要保存到的文件夹的 IIS 用户权限

标签: c# asp.net-mvc file-upload


【解决方案1】:

好像是权限问题

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-31
    • 2015-11-22
    • 2013-01-29
    • 2015-09-18
    • 1970-01-01
    • 1970-01-01
    • 2020-03-30
    • 2014-12-04
    相关资源
    最近更新 更多