【问题标题】:Uploading image in ASP.NET MVC在 ASP.NET MVC 中上传图像
【发布时间】:2012-05-11 05:44:47
【问题描述】:

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

这是我的控制器代码:

[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(); 设置图像名称?或者如何设置图像路径?

【问题讨论】:

标签: c# .net asp.net-mvc asp.net-mvc-3 razor


【解决方案1】:

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

<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
    }
}

【讨论】:

  • 我使用您的代码,我认为它可以正常工作,但它向我显示一个错误:拒绝访问路径 'C:\Users\Administrator\Desktop\ND\MyApp\MyApp\Uploads' .你知道为什么吗 ?为什么它在本地向我显示此错误?
  • 嗯,您需要从网站的应用程序池中找出它正在运行的身份(默认为应用程序池身份)并授予正确的权限。
  • 我将我的应用程序池标识更改为 Localsystem .. 我在某处发红说我必须将其更改为 localsystem 但它不起作用.. 有什么建议吗?
  • 嗯,看这里learn.iis.net/page.aspx/624/application-pool-identities 并确保你已经按照所有这些步骤,应该可以正常工作:)
  • @Persian 好东西!很高兴我能帮忙:)
【解决方案2】:

您可以使用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
   }
 }
}

如果你想通过ajax上传文件而不刷新页面,那么你可以使用this article which uses jquery plugin

【讨论】:

    猜你喜欢
    • 2011-06-18
    • 2017-10-20
    • 1970-01-01
    • 2010-11-25
    • 2018-09-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-08-27
    相关资源
    最近更新 更多