简体   繁体   中英

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. But how can i convert System.Byte[] to a posted file inside html input ?

Actually im doing like this in my 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:

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:

<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.

But on page ready, my input[type=file] still empty, showing the message "No file selected".

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:

--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. This plugin might be of interest

Thanks everyone!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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