public HttpResponseMessage UploadFile() { foreach (string file in Request.Files) { var FileDataContent = Request.Files[file]; if (FileDataContent != null && FileDataContent.ContentLength > 0) { var stream = FileDataContent.InputStream; var fileName = Path.GetFileName(FileDataContent.FileName); try { System.IO.BinaryReader br = new System.IO.BinaryReader(stream); Byte[] bytes = br.ReadBytes((Int32)stream.Length); string base64String = Convert.ToBase64String(bytes, 0, bytes.Length); WriteFileToDB(fileName, base64String); Shared.Utils UT = new Shared.Utils(); UT.MergeFile(fileName); } catch (Exception ex) { // handle } } } return(new HttpResponseMessage() { StatusCode = System.Net.HttpStatusCode.OK, Content = new StringContent("File uploaded.") }); }
public HttpResponseMessage UploadFile() { foreach (string file in Request.Files) { var FileDataContent = Request.Files[file]; if (FileDataContent != null && FileDataContent.ContentLength > 0) { // take the input stream, and save it to a temp folder using the original file.part name posted var stream = FileDataContent.InputStream; var fileName = Path.GetFileName(FileDataContent.FileName); var UploadPath = Server.MapPath("~/File"); Directory.CreateDirectory(UploadPath); string path = Path.Combine(UploadPath, fileName); try { if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } using (var fileStream = System.IO.File.Create(path)) { stream.CopyTo(fileStream); } // Once the file part is saved, see if we have enough to merge it Shared.Utils UT = new Shared.Utils(); UT.MergeFile(path); } catch (IOException ex) { // handle } } } return(new HttpResponseMessage() { StatusCode = System.Net.HttpStatusCode.OK, Content = new StringContent("File uploaded.") }); }
public ActionResult UploadFileWithSplits(Guid?articleId, IEnumerable <HttpPostedFileBase> files) { int i = files.Count(); for (int n = 0; n < i; n++) { var FileDataContent = Request.Files[n]; if (FileDataContent != null && FileDataContent.ContentLength > 0) { var stream = FileDataContent.InputStream; var fileName = Path.GetFileName(FileDataContent.FileName); var UploadPath = Server.MapPath(UploadFolder + User.Identity.Name.Replace("@", "_")); if (!Directory.Exists(UploadPath)) { Directory.CreateDirectory(UploadPath); } try { string path = Path.Combine(UploadPath, fileName); if (System.IO.File.Exists(path)) { System.IO.File.Delete(path); } using (var fileStream = System.IO.File.Create(path)) { stream.CopyTo(fileStream); } if (!path.IsNullOrEmptyOrWhiteSpace()) { // Once the file part is saved, see if we have enough to merge it Shared.Utils UT = new Shared.Utils(path); UT.TempFolder = UploadPath; UT.SplitFile(path); UT.MergeFile(path); try { FileModel _file = new FileModel(path); _file.CreatedBy = User.Identity.Name; if (articleId.HasValue) { _file.ArticleId = articleId.Value; _file.CreatedBy = User.Identity.Name; if (!_articleManager.AddFile(articleId.Value, _file)) { //System.IO.File.Delete(path); } } } catch { Console.WriteLine(); } } } catch (IOException ex) { Console.WriteLine(ex.Message); return(View("Errors")); } } } if (Request.IsAjaxRequest()) { return(PartialView("_UploadCompleted")); } return(View("_UploadCompleted")); }