在 ASP.NET MVC 中上传图像 [英] Uploading image in ASP.NET MVC

查看:25
本文介绍了在 ASP.NET MVC 中上传图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是我的控制器代码:

[HttpPost]公共操作结果创建(tblPortfolio tblportfolio){如果(模型状态.IsValid){db.tblPortfolios.AddObject(tblportfolio);db.SaveChanges();return RedirectToAction("索引");}返回视图(tblportfolio);}

这是我的视图代码:

@model MyApp.Models.tblPortfolio<h2>创建</h2>@using (Html.BeginForm(null, null, FormMethod.Post, new { enctype = "multipart/form-data" })){@Html.ValidationSummary(true)<字段集><legend>tblPortfolio</legend><div class="editor-label">@Html.LabelFor(model => model.Title)

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

<div class="editor-label">@Html.LabelFor(model => model.ImageFile)

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

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

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

<p><输入类型=提交"值=创建"/></p></fieldset>}

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

解决方案

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

然后您需要更改您的帖子 ActionMethod 以采用 HttpPostedFileBase,如下所示:

[HttpPost]公共 ActionResult 创建(tblPortfolio tblportfolio,HttpPostedFileBase 文件){//你可以把你现有的保存代码放在这里if (file != null && file.ContentLength > 0){//对文件做任何你想做的事}}

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 ?

解决方案

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 ActionMethod to take a HttpPostedFileBase, like so:

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

这篇关于在 ASP.NET MVC 中上传图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
C#/.NET最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆