简体   繁体   English

如何在编辑页面ASP.NET MVC中为HttpPostedFileBase创建输入类型文件?

[英]How to create input type file for HttpPostedFileBase in edit page ASP.NET MVC?

I met an issue during developing a MVC application. 在开发MVC应用程序时遇到了一个问题。

I started with simple model which is used for a view, like: 我从用于视图的简单模型开始,例如:

public class MyModel 
{
    public HttpPostedFileBase[] Files { get; set; }

    public string Name { get; set; }
}

I have a view (Create.aspx) 我有一个视图(Create.aspx)

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MyModel>" %>
...
    <form method="post" action="/My/Create" enctype = "multipart/form-data">
        <input type="file" name="Files" />
        ...
        <%:Html.TextBoxFor(item=>item.Name) %>
        <input type="submit" value="Create" />
    </form>

The controller: 控制器:

public class ActionController: Controller {
    public ActionResult Create() {  
        var myModel = new MyModel(); 
        return View(model); 
    }

    [HttpPost]
    public ActionResult Create(MyModel model) {
        if(ModelState.isValid) { /* save */ }
    }

    public ActionResult Edit(int id) {  
        var myModel = _manager.Get(id);
        myModel.Files = /* what to do here ? */ 

        return View(model); 
    }

    [HttpPost]
    public ActionResult Edit(MyModel model) {
        if(ModelState.isValid) { /* edit */ }
    }
} 

My question : How to create HttpPostedFileBase object to be send to view and display them (see below Edit page) ? 我的问题 :如何创建要发送的HttpPostedFileBase对象以查看和显示它们(请参见下面的“编辑”页面)?

The files are stored in database as nvarchar with relative path to them (as label). 文件以nvarchar的形式存储在数据库中,并带有它们的相对路径(作为标签)。

I want to preserve already saved file and just change the name field. 我想保留已经保存的文件,只是更改名称字段。

From database I receive an object which stores file path, the type of file and a file stream. 从数据库中,我收到一个对象,该对象存储文件路径,文件类型和文件流。

And for Edit aspx page: 对于Edit aspx页面:

<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<MyModel>" %>
...
    <form method="post" action="/My/Edit" enctype = "multipart/form-data">
        <input type="file" name="Files" />
        /* display here the files */
        ...
        <%:Html.TextBoxFor(item=>item.Name) %>
        <input type="submit" value="Create" />
    </form>

You simply cannot use HttpPostedFileBase to display your files. 您根本无法使用HttpPostedFileBase显示文件。 As its description says: 如其描述所述:

Serves as the base class for classes that provide access to individual files that have been uploaded by a client. 用作提供对客户端上载的单个文件的访问权的类的基类。

It is used to access files that are being uploaded. 它用于访问正在上传的文件。 If you want to display files that have been uploaded, then you have to use some other approach like using File methods . 如果要显示已上传的文件,则必须使用其他方法,例如使用File方法

You can show file on view using code like an example below: 您可以使用以下示例代码在视图上显示文件:

    public FileContentResult ViewImage(/*parameters*/)
    {
        byte[] img = //TODO: take your image as byte array
        return new FileContentResult(img, "image/jpeg");
    }

and in your view : 并且在您view

<img src="@Url.Action("ViewImage", "ControllerName", new { @*parameters*@ })">

But if you want just preview files before upload you have to use something like this: 但是,如果您只想在上传之前预览文件,则必须使用以下命令:

HTML: HTML:

<p>Please specify an image file: <input type="file" name="datafile" id="datafile" size="40"></p>
<div id="preview"><img height="240" /></div>

JS: JS:

$(function() {
   $('#datafile').change(function(e)
       {
           e.preventDefault();
           var f = e.target.files[0];
           if(f && window.FileReader)
           {
               var reader = new FileReader();
               reader.onload = function(evt) { $('#preview>img').attr('src', evt.target.result); };
               reader.readAsDataURL(f);
           }
       }
   );
});

FIDDLE 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM