上传并保存在ASP.NET MVC4互联网应用检索图像和视频的网址 [英] Uploading and retrieving image and video url in ASP.NET MVC4 Internet Application

查看:127
本文介绍了上传并保存在ASP.NET MVC4互联网应用检索图像和视频的网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下的模型在我的MVC 4互联网应用程序

I have the following model in my MVC 4 Internet Application

public class EventModel
{
    public int EventId { get; set;}
    public string EventTitle { get; set;}
    public string ImageUrl { get; set;}
    public string VideoUrl { get;set}
}

这里的Create.cshtml code的图像和视频

Here's the Create.cshtml code for the Image and Video

@using(Html.BeginForm())
{
@Html.AntiForgeryToken()
@Html.ValidationSummary(true)

<div>
@Html.LabelFor(model =>model.ImageUrl)
</div>
<div>
@Html.EditorFor(model =>model.ImageUrl)
@Html.ValidationMessageFor(model =>model.ImageUrl)
</div>

<div>
@Html.LabelFor(model =>model.VideoUrl)
</div>
<div>
@Html.EditorFor(model =>model.VideoUrl)
@Html.ValidationMessageFor(model =>Video.ImageUrl)
</div>

}

这是我的控制器code上创建POST方法

This is my controller code for the post method on create

[HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult Create(EventModel eventmodel)
        {
            if (ModelState.IsValid)
            {
                _db.EventModels.Add(eventmodel);
                _db.SaveChanges();
                return RedirectToAction("Index");
            }

            return View(eventmodel);
        }

我怎样才能使创建页面显示的按钮从用户电脑上传图片。如何修改我的控制器行动路径存储数据库,然后我怎么找回index.cshtml页上的图像。

How can I make the create page display buttons to upload image from the users computer. How do i modify my controller action to store the paths on the database and then how do I retrieve the image on the index.cshtml page.

推荐答案

有关文件上传试试这个:

For file uploading try this:

在控制器:

[HttpPost]
public ActionResult Create(EventModel eventmodel, HttpPostedFileBase file)
{ 
   if (ModelState.IsValid)
   {
      var filename = Path.GetFileName(file.FileName);
      var path = Path.Combine(Server.MapPath("~/Uploads/Photo/"), filename);
      file.SaveAs(path);
      tyre.Url = filename;

      _db.EventModels.AddObject(eventmodel);
      _db.SaveChanges();
      return RedirectToAction("Index");
   }
   return View(eventmodel);
}

和查看:

<div>
   Image
   <input type="file" name="file" id="file" />
   @Html.HiddenFor( model => model.ImageUrl)
   @Html.ValidationMessageFor( model => model.Url )
</div>

这篇关于上传并保存在ASP.NET MVC4互联网应用检索图像和视频的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆