简体   繁体   English

将System.Byte []转换为HTML Input type =“ FILE”

[英]Convert System.Byte[] to HTML Input type=“FILE”

I need to persist data of a Posted file, because in case of validation errors on POST, and the page reloads, the user dont need to post the files again, as there may be many. 我需要保留已发布文件的数据,因为在POST上发生验证错误的情况下,并且页面重新加载时,用户不需要再次发布文件,因为可能有很多。 But how can i convert System.Byte[] to a posted file inside html input ? 但是,如何将System.Byte []转换为html输入中的发布文件?

Actually im doing like this in my Controller.cs: 实际上我在Controller.cs中这样做:

var binaryReader = new BinaryReader(
                            HttpContext.Current.Request.Files["INPUT_NAME"].InputStream))

After convert my HttpPostedFileBase to BinaryReader, i set the value on my ViewModel property (campo.ByteArray), as the name say, its Byte[] type: 将我的HttpPostedFileBase转换为BinaryReader之后,我在ViewModel属性(campo.ByteArray)上设置了值,顾名思义,其Byte []类型:

campo.ByteArray =
                        binaryReader.ReadBytes(
                            HttpContext.Current.Request.Files["INPUT_NAME"].ContentLength);

And then send it to my View.cshtml, and trying to set this value to the input[type=file] like this: 然后将其发送到我的View.cshtml,并尝试将此值设置为input [type = file],如下所示:

<input type="file" name="INPUT_NAME" id="INPUT_NAME" value="@Model.ByteArray"/>

Considering my @Model.ByteArray as the byteArray created on controller using the HttpPostedFileBase. 考虑我的@ Model.ByteArray作为使用HttpPostedFileBase在控制器上创建的byteArray。

But on page ready, my input[type=file] still empty, showing the message "No file selected". 但是在页面准备就绪时,我的输入[type = file]仍然为空,显示消息“未选择文件”。

Is there a way to make it possible ? 有没有办法使之成为可能?

Thanks in advance! 提前致谢!

As explained by @Stephen Muecke above, there's security reasons that dont allow i to do this: 正如上面@Stephen Muecke所解释的,出于安全原因,我无法这样做:

--You cannot set the value of a file input for security reasons. -出于安全原因,您无法设置文件输入的值。 It can only be set by a user selecting a file in the browser --If you need to return the view because ModelState is invalid, temporarily save the files, and return their file names so that the user can see what has been already uploaded. 只能由用户在浏览器中选择一个文件来设置它-如果由于ModelState无效而需要返回视图,请临时保存文件并返回其文件名,以便用户可以查看已经上传的文件。 This plugin might be of interest 该插件可能很有趣

Thanks everyone! 感谢大家!

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

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