简体   繁体   中英

ASP.NET MVC5 copy HTTP Post File from temporary memory to server

As a relatively new to asp.net and do not have a great knowledge of all of it features and capabilities. I would like to get some advice from more experienced users.

I am trying to find the best way to automatically upload files to from 1 or more remote computers to a file server through a server side script (Java most probably). The server is running windows server 2012 and hosts a ASP.NET MVC website. Unfortunately FTP is not an option due to our IT infrastructure.

I have been investigating file transfer using HTTP POST/PUT & GET methods between remote computers and file store server using the website as the gateway. I have successfully implemented a test project to upload a file to the server from the ASP.NET Webpage webpage when a user selects a file and clicks submit button. I would now like to extend that to when the webpage Handles the HTTP Post request it copies it from temporary memory to the server.

Any comments help is greatly appreciated.

Regards,

Tim

This is the Model.cs

 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.IO; using amrcadpWebDev.Controllers; namespace amrcadpWebDev.Models { public class FileModels { public HttpPostedFileBase amrcadpFile { get; set; } } } 

This is the FileController.cs

 using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.IO; using System.Linq; using System.Web; using System.Web.Mvc; namespace amrcadpWebDev.Controllers { /// <summary> /// File Controller /// </summary> public class FileController : Controller { public ActionResult Index() { return View(); } #region Action [HttpPost] public virtual ActionResult FileUpload(HttpPostedFileBase File) { bool isUploaded = false; string message = "File upload failed"; if (ModelState.IsValid) { if (File != null && File.ContentLength != 0 ) { string pathForSaving = "\\\\\\\\*****\\\\FileStore\\\\resultsFiles\\\\"; string fname = Path.Combine(pathForSaving, File.FileName); try { File.SaveAs(Path.Combine(pathForSaving, File.FileName)); isUploaded = true; message = "File uploaded successfully!"; } catch (Exception ex) { message = string.Format("File upload failed!!: {0} {1}", ex.Message, fname); } } return Json(new { isUploaded = isUploaded, message = message }, "text/html"); } return View(); } #endregion } } 

This is the view/Index.cs

 @model amrcadpWebDev.Models.FileModels <h2>Basic File Upload</h2> @using (Html.BeginForm ("FileUpload", "File", FormMethod.Post, new { enctype = "multipart/form-data" })) { <label for="file">Upload File:</label> <input type="file" name="file" id="file" /><br><br> <input type="submit" value="Upload File" /> <br><br> <label for="file">cRIO File String:</label> <input type="submit" value="Get File" /> <p> @Html.ActionLink("get cRIO", "GetFile") </p> @ViewBag.Message } @Html.DisplayNameFor(model => model.amrcadpFile) 

This works for a user initiated file select and submit. How would i adapt this to manage a http post with file data which i would then save from temporary memory to the server.

I hope my explanation is clear any question please comment.

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