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