C# 在 ASP.NET MVC 中上传图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10402094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 13:42:25  来源:igfitidea点击:

Uploading image in ASP.NET MVC

c#.netasp.net-mvcasp.net-mvc-3razor

提问by Persian.

I have a Upload form and I want to pass my information such as an Image and some other field but I don't know how can I upload Image ..

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

this is my controller code :

这是我的控制器代码:

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

            return View(tblportfolio);
        }

And this is my view code :

这是我的视图代码:

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

Now I don't know how can I upload Image and save it on server .. how can I set Image name by Guid.NewGuid();? Or how can I set Image Path ?

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

采纳答案by mattytommo

Firstly, you'll need to change your view to include the following:

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

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

Then you'll need to change your post ActionMethodto take a HttpPostedFileBase, like so:

然后您需要更改您的帖子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
    }
}

回答by Ehsan Sajjad

You can get it from Requestusing Request.FilesCollection, In case of single file upload just read from the first index using Request.Files[0]:

您可以Request使用Request.FilesCollection获取它,如果上传单个文件,只需使用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  
 }
 } 
}

In case of Multiple files uploading, you have to iterate on the Request.Filescollection:

在上传多个文件的情况下,您必须迭代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
   }
 }
}

If you want to upload file without page refresh via ajax then you can use this article which uses jquery plugin

如果你想通过ajax上传文件而不刷新页面,那么你可以使用这篇使用jquery插件的文章