在ASP.NET MVC中上传图片

41

我有一个上传表单,我想传递一些信息,例如图片和其他字段,但我不知道如何上传图片..

这是我的控制器代码:

[HttpPost]
        public ActionResult Create(tblPortfolio tblportfolio)
        {
            if (ModelState.IsValid)
            {
                db.tblPortfolios.AddObject(tblportfolio);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(tblportfolio);
        }

这是我的视图代码:

@model MyApp.Models.tblPortfolio

<h2>Create</h2>

@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" }))
{
    @Html.ValidationSummary(true)
    <fieldset>
        <legend>tblPortfolio</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.Title)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Title)
            @Html.ValidationMessageFor(model => model.Title)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.ImageFile)
        </div>
        <div class="editor-field">
            @Html.TextBoxFor(model => model.ImageFile, new { type = "file" })
            @Html.ValidationMessageFor(model => model.ImageFile)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.Link)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.Link)
            @Html.ValidationMessageFor(model => model.Link)
        </div>

        <p>
            <input type="submit" value="Create" />
        </p>
    </fieldset>
}

现在我不知道该如何上传图像并将其保存到服务器上...我如何使用Guid.NewGuid();来设置图像名称?或者我如何设置图像路径?


2
model.ImageFile的类型是什么? - Shimmy Weitzhandler
@Shimmy:我只是在数据库中保存了图像名称。它是一个字符串。 - Persian.
我最终为每个新图像生成了一个GUID,并将其名称保存在数据库中。文件夹并未保存到服务器上,只有图像文件名被保存。该文件夹是动态注入的。 - Shimmy Weitzhandler
1
在ASP.NET MVC 4 Razor中上传多个文件,还提供了客户端验证。 - Sender
请查看我的回答:https://dev59.com/Am435IYBdhLWcg3w0Trc#40990080 - Basheer AL-MOMANI
2个回答

45

首先,您需要更改视图以包含以下内容:

<input type="file" name="file" />

然后你需要将你的提交 ActionMethod 更改为接受一个 HttpPostedFileBase,就像这样:

[HttpPost]
public ActionResult Create(tblPortfolio tblportfolio, HttpPostedFileBase file)
{
    //you can put your existing save code here
    if (file != null && file.ContentLength > 0) 
    {
        //do whatever you want with the file
    }
}

1
我使用了你的代码,我认为它能正常工作,但是它向我显示了一个错误:访问路径“C:\Users\Administrator\Desktop\ND\MyApp\MyApp\Uploads”被拒绝。你知道为什么吗?为什么它在本地向我显示这个错误? - Persian.
1
嗯,你需要从网站的应用程序池中找出它正在运行的身份(默认情况下为应用程序池身份),并授予正确的权限。 - Mathew Thompson
1
我将我的应用程序池身份更改为Localsystem。我在某个地方读到过,必须将其更改为localsystem,但它没有起作用。有什么建议吗? - Persian.
2
嗯,看这里http://learn.iis.net/page.aspx/624/application-pool-identities/,确保你已经按照所有步骤操作了,那应该就可以正常工作了 :) - Mathew Thompson
@波斯语 不错的东西!很高兴我能帮到你 :) - Mathew Thompson
这里有更多信息:http://haacked.com/archive/2010/07/16/uploading-files-with-aspnetmvc.aspx - John Zumbrum

3

您可以使用Request.Files集合从Request中获取它,如果只上传单个文件,请使用Request.Files[0]从第一个索引读取:

[HttpPost]
public ActionResult Create(tblPortfolio tblportfolio) 
{
 if(Request.Files.Count > 0)
 {
 HttpPostedFileBase file = Request.Files[0];
 if (file != null) 
 { 
  // business logic here  
 }
 } 
}

在多个文件上传的情况下,您需要对Request.Files集合进行迭代:
[HttpPost] 
public ActionResult Create(tblPortfolio tblportfolio)
{ 
 for(int i=0; i < Request.Files.Count; i++)
 {
   HttpPostedFileBase file = Request.Files[i];
   if (file != null)
   {
    // Do something here
   }
 }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接