There is a PartialView, in which there is a button, when clicked, a modal window is displayed using ajax

<div id="Dialog" class="modal fade"> <div id="dialogContent" class="modal-dialog"></div></div> @Html.ActionLink("Загрузить фото", "LoadPhoto", null, new { @class = "my-button-2", @id = "loadphoto" }) <script type="text/javascript"> $(function () { $.ajaxSetup({ cache: false }); $("#loadphoto").click(function (e) { e.preventDefault(); $.get(this.href, function (data) { $('#dialogContent').html(data); $('#Dialog').modal('show'); }); }); }); 

_LoadPhoto.cshtml

 <div class="modal-content"> <div class="modal-header"> <button class="close" data-dismiss="modal" area-hidden="true">X</button> <p>Загрузите фото:</p> </div> <div class="modal-body"> @using (Html.BeginForm("FileUpload", "Employees", FormMethod.Post, new { enctype = "multipart/form-data" })) { <label for="file">Выберите фото:</label> <input type="file" name="file" id="file" style="width: 100%;" /> <input type="submit" value="Upload" class="submit" /> } </div> 

Post-post method

 public ActionResult FileUpload(HttpPostedFileBase file) { if (file != null) { string pic = Path.GetFileName(file.FileName); string path = Path.Combine( Server.MapPath("~/images"), pic); // file is uploaded file.SaveAs(path); // save the image path path to the database or you can send image // directly to database // in-case if you want to store byte[] ie. for DB using (MemoryStream ms = new MemoryStream()) { file.InputStream.CopyTo(ms); byte[] array = ms.GetBuffer(); } } // after successfully uploading redirect the user return RedirectToAction("Index"); } 

All this is pulled from the Anglo-stack.

Question : How or where to save the downloaded file in the form of byte[] , then to use on the page from which there was a call?

The call was from the model creation page. Those. After loading the image in the model being edited, the photo property will be filled with the byte[] value that was loaded.

Just in case the model

 public class Employee { ... public virtual ManInfo ManInfo { get; set; } ... } public class ManInfo { ... public byte[] Photo { get; set; } ... } 

    1 answer 1

    Where to store byte [] array depends on your application. You can save in the database, you can - in a temporary file. The main thing is to set an identifier for the file, which can be found in the display. For a registered user, this can be a user code, for an anonymous user, an anonymous identifier.

    • Little did not understand you. I need to use this array in another method. But it should be noted that the method with the file download may be skipped. I tried to store an array at the class level - for some reason it vanishes. - bodynar
    • If you pass byte [] as a parameter in one request to the web server, the information will not be lost. If you are talking about the use of the parameter between calls to the web server, then the array should be stored in intermediate storage. With each access, the controller object is re-created, so your array is “nullified” at the class level. The closest alternative is session variable. - Sergey Kartashov
    • In principle, I thought so. During the time elapsed since the question, I slightly changed the structure and now the selected file must be transferred using jquery in the ajax request to the post-method of the controller in order to record the data in the database. The problem is that I have the <input type="file"/> in which the file is selected. click to send it (size validation is already configured and functioning) - bodynar