c# - mvc 将文件保存到站点中的文件夹

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

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

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

最佳答案

看起来像是权限问题

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

关于c# - mvc 将文件保存到站点中的文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11847493/

相关文章:

c# - Profiler 报警统计

c# - 防止显示 Windows 安全窗口

asp.net-mvc - 使用 ScriptIgnore 时的 JavaScriptSerializer 循环引用

Jquery 对话框在 MVC 中不起作用

asp.net-mvc-3 - 在 MVC3 中检查上传文件是否有病毒

html - <输入类型 ="file"> 问题

java - 不朽之物真的存在吗?

asp.net-mvc - asp.net mvc - 值(value)提供者和集合

java - 如何从 Android 上传 PDF 文件?

c# - 确保从特定链接访问页面