private void button1_Click(object sender, EventArgs e) { StringBuilder builder = new StringBuilder(); builder.Append(default(Guid).ToString()); builder.Append("\n"); builder.Append(Guid.NewGuid().ToString()); builder.Append("\n"); builder.Append(DateTime_Helper_DG.GetCurrentTimeStamp().ToString()); builder.Append("\n"); builder.Append(DateTime_Helper_DG.Get_DateTime_Now_24HourType()); builder.Append("\n"); builder.Append(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); builder.Append("\n"); richTextBox_OutPut.Text = builder.ToString(); }
//POST : api/File public async Task <IHttpActionResult> Post() { string SAVE_DIR = UPLOAD_DIR; //save folder string root = Path.Combine(IO_Helper_DG.RootPath_MVC, SAVE_DIR); //check path is exist if not create it IO_Helper_DG.CreateDirectoryIfNotExist(root); IList <string> fileNameList = new List <string>(); StringBuilder sb = new StringBuilder(); long fileTotalSize = 0; int fileIndex = 1; //get files from request HttpFileCollection files = HttpContext.Current.Request.Files; await Task.Run(() => { foreach (var f in files.AllKeys) { HttpPostedFile file = files[f]; if (!string.IsNullOrEmpty(file.FileName)) { string fileLocalFullName = Path.Combine(root, file.FileName); file.SaveAs(fileLocalFullName); fileNameList.Add(Path.Combine(SAVE_DIR, file.FileName)); FileInfo fileInfo = new FileInfo(fileLocalFullName); fileTotalSize += fileInfo.Length; sb.Append($" #{fileIndex} Uploaded file: {file.FileName} ({ fileInfo.Length} bytes)"); fileIndex++; Trace.WriteLine("1 file copied , filePath=" + fileLocalFullName); } } }); return(OK($"{fileNameList.Count} file(s) /{fileTotalSize} bytes uploaded successfully! Details -> {sb.ToString()} -- qx_frame {DateTime_Helper_DG.Get_DateTime_Now_24HourType()}", fileNameList, fileNameList.Count)); }
//POST : api/Picture/id public async Task <IHttpActionResult> Post(string id) { string SAVE_DIR = string.IsNullOrEmpty(id) ? UPLOAD_DIR : Path.Combine(UPLOAD_DIR, id); //check file type if (!Request.Content.IsMimeMultipartContent()) { throw new Exception_DG("unsupported media type", 2005); } string dir = Path.Combine(IO_Helper_DG.RootPath_MVC, "/temp"); IO_Helper_DG.CreateDirectoryIfNotExist(dir); var provider = new MultipartFormDataStreamProvider(dir); // Read the form data. await Request.Content.ReadAsMultipartAsync(provider); List <string> fileNameList = new List <string>(); StringBuilder sb = new StringBuilder(); long fileTotalSize = 0; int fileIndex = 1; // This illustrates how to get the file names. foreach (MultipartFileData file in provider.FileData) { //new folder string saveDir = Path.Combine(IO_Helper_DG.RootPath_MVC, SAVE_DIR); IO_Helper_DG.CreateDirectoryIfNotExist(saveDir); if (File.Exists(file.LocalFileName)) { //get file name string fileName = file.Headers.ContentDisposition.FileName.Substring(1, file.Headers.ContentDisposition.FileName.Length - 2); //create new file name string newFileName = String.Concat(Guid.NewGuid(), ".", fileName.Split('.')[1]); string newFullFileName = Path.Combine(saveDir, newFileName); fileNameList.Add(Path.Combine(SAVE_DIR, newFileName)); FileInfo fileInfo = new FileInfo(file.LocalFileName); fileTotalSize += fileInfo.Length; sb.Append($" #{fileIndex} Uploaded file: {newFileName} ({ fileInfo.Length} bytes)"); fileIndex++; File.Move(file.LocalFileName, newFullFileName); Trace.WriteLine("1 file copied , filePath=" + newFullFileName); } } return(OK($"{fileNameList.Count} file(s) /{fileTotalSize} bytes uploaded successfully! Details -> {sb.ToString()} -- qx_frame {DateTime_Helper_DG.Get_DateTime_Now_24HourType()}", fileNameList, fileNameList.Count)); }