简体   繁体   English

页面刷新后,在ASP.Net MVC视图中保留输入类型文件的值

[英]Retain value of input type file in a ASP.Net MVC view after page refresh

I am using a input type file in MVC view form whose value is posted to action method for performing some validation. 我正在MVC视图格式中使用输入类型文件,其值发布到操作方法中以执行一些验证。 The input file value is binded to a property in model which will be returned after page refresh. 输入文件的值绑定到模型中的属性,页面刷新后将返回该属性。 But file value is not getting binded automatically after page refresh.Please find below the code. 但是刷新页面后不会自动绑定文件值。请在代码下方找到。

This is my view code. 这是我的查看代码。

@model WebApplication10.Models.Model1

<h2>HtmlToPDF</h2>
@using (Html.BeginForm("HtmlToPDF", "Home", FormMethod.Post, new { enctype = 
"multipart/form-data" }))
{
    <table>
        <tr>
            <td>File</td>
            <td><input type="file" name="File1" value="@Model.File1"/></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit" value="ToHTML" /></td>
        </tr>
    </table>
}

Controller code: 控制器代码:

[HttpPost]
    public ActionResult HtmlToPDF(Model1 file)
    {
        //Some validation.
        return View(file);
    }

and this is my model: 这是我的模型:

public class Model1
{
    public HttpPostedFileBase File1 { get; set; }
}

Any help will be appreciated. 任何帮助将不胜感激。

  1. Due to security reason browser will not allow you to set that information. 出于安全原因,浏览器不允许您设置该信息。 Once Form Submit input of type=file get cleared and it is not set manually by script or other programming stuff. 一旦清除了类型=文件的表单提交输入,并且脚本或其他编程人员没有手动设置它。

  2. In your case you can do something like this. 在您的情况下,您可以执行以下操作。 When Form Is Submitted , you can hold Model1 property File1 information in some other object. 提交表单后,可以将Model1属性File1信息保存在其他对象中。 Like FileName , FileSize and StreamOfData. 像FileName,FileSize和StreamOfData一样。 You pass marker to Form that you have stored that data. 您将标记传递给已存储该数据的Form。 So when next time Form submitted you can consider that data if any new data is not passed. 因此,下次提交表单时,如果没有传递任何新数据,您可以考虑该数据。

I hope this suggestion will help. 我希望这个建议会有所帮助。

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

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