public void ProcessRequest(HttpContext context) { using (var txscope = new TransactionScope(TransactionScopeOption.RequiresNew)) { try { if (context.Request.Files.Count > 0) { string RefNo = context.Request.Form["RefNo"].ToString(); string filePath = context.Request.Form["filePath"].ToString(); string fileType = context.Request.Form["fileType"].ToString(); string strNewFolder = filePath; NameValueCollection arr = context.Request.Form; HttpFileCollection files = context.Request.Files; string newfileName = ""; string fname = ""; if (RefNo != "" && files.Count > 0) { List <CommonUploadFile> lstDoc = new List <CommonUploadFile>(); for (int i = 0; i < files.Count; i++) { HttpPostedFile file = files[i]; string unqFileName = GlobalFunctions.GetUniqueFileName(file.FileName); newfileName = RefNo + '_' + (new GlobalFunctions()).GetUniqueFilename(strNewFolder, unqFileName); fname = context.Server.MapPath("~/" + strNewFolder + '/' + newfileName); file.SaveAs(fname); string fileNo = ""; string LetterNo = ""; CommonUploadFile objDoc = new CommonUploadFile(); objDoc.FileId = Convert.ToInt32(arr[0]); objDoc.UniqueFileName = newfileName; objDoc.FileName = file.FileName; objDoc.FilePath = strNewFolder; objDoc.FileSpec = arr[0]; objDoc.FileNo = fileNo; objDoc.LetterNo = LetterNo; objDoc.RefNo = RefNo; lstDoc.Add(objDoc); } string strDocs = Newtonsoft.Json.JsonConvert.SerializeObject(lstDoc); txscope.Complete(); context.Response.Write(strDocs); } else { txscope.Dispose(); context.Response.Write("NoFiles"); } } else { txscope.Dispose(); context.Response.Write("SizeExceed"); } } catch (Exception ex) { txscope.Dispose(); context.Response.Write("Error"); } } }
private void TransferSingleAttachmentFile(FileInfo fileInfo, BaseAttachmentDto attachmentDto) { long sentFileSize = fileInfo.Length; long writtenFileSize = 0; CmsWebServiceClient cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint); try { Stream stream = fileInfo.OpenRead(); if (stream.Length > int.MaxValue) { DbImportResult r = new DbImportResult(); r.ErrorMessages = new List<string>(); r.ErrorMessages.Add(string.Format("File [{0}] can not be bigger than 2GB. ", fileInfo.Name)); FileTransferFailed(r); return; } int transferChunkSize = Utils.GetTransferChunkSize(stream); CommonUploadFile file = new CommonUploadFile { Name = fileInfo.Name, Size = stream.Length, Path = attachmentDto.Path }; if (stream.Position > -1 && stream.Position < stream.Length) { int chunkSize = Utils.GetCurrentChunkSize(stream, transferChunkSize); byte[] fileBytes = new byte[chunkSize]; stream.Read(fileBytes, 0, chunkSize); cmsWebServiceClient.UploadFileCompleted += (s1, e1) => { if (stream.Position > -1 && stream.Position < stream.Length) { chunkSize = Utils.GetCurrentChunkSize(stream, transferChunkSize); fileBytes = new byte[chunkSize]; stream.Read(fileBytes, 0, chunkSize); writtenFileSize += e1.Result.EntityResult.WrittenByteArraySize; cmsWebServiceClient.UploadFileAsync(file.Name, fileBytes, CMS.AppSetting.AttachmentsRootUrl, file.Path, true); } else { //Close the stream when all files are uploaded stream.Close(); writtenFileSize += e1.Result.EntityResult.WrittenByteArraySize; if (writtenFileSize < sentFileSize) { DbImportResult r = new DbImportResult(); r.ErrorMessages = new List<string>(); r.ErrorMessages.Add("There was a loss of information while attempting to transfer the file. Please try again. "); FileTransferFailed(r); return; } BeginAttachmentInsert(attachmentDto); } }; cmsWebServiceClient.UploadFileAsync(file.Name, fileBytes, CMS.AppSetting.AttachmentsRootUrl, file.Path, false); } } catch (Exception ex) { mGlobalImportResult.ErrorMessages.Add(ex.Message); } }
private void ProccessTransferFiles(int fileNumber, FileInfo dialogFile, List<FileInfo> dialogFiles) { var cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint); try { Stream stream = dialogFile.OpenRead(); View.RadProgressBar1.Maximum = stream.Length; View.RadProgressBar1.Minimum = 0; View.RadProgressBar1.Visibility = Visibility.Visible; View.RadProgressBar1.Value = 0; if (stream.Length > int.MaxValue) { var errorDialog = new PopupDialog(PopupDialogType.Warning, string.Format("File [{0}] can not be bigger than 2GB.", dialogFile.Name), "Warning: File is to large"); errorDialog.Show(); return; } int transferChunkSize = Utils.GetTransferChunkSize(stream); var file = new CommonUploadFile {Name = dialogFile.Name, Size = stream.Length, Path = Guid.NewGuid().ToString()}; var newAttachment = new IssueFile { DateUploaded = DateTime.Now, Filename = file.Name, IssueId = mIssue.Id, Issue = mIssue, Path = file.Path, UploadedById = CMS.User.Id, User = CMS.User, AttachmentType = new AttachmentType {Id = (int) CommonUtils.AttachmentType.Unknown}, AttachmentTypeId = (int) CommonUtils.AttachmentType.Unknown, AttachmentTypes = mAttachmentTypes, RestrictedContent = false, SelectedAttachmentType = new AttachmentType {Id = (int) CommonUtils.AttachmentType.Unknown} }; file.Attachment = newAttachment; if (stream.Position > -1 && stream.Position < stream.Length) { int chunkSize = Utils.GetCurrentChunkSize(stream, transferChunkSize); var fileBytes = new byte[chunkSize]; stream.Read(fileBytes, 0, chunkSize); EventHandler<UploadAttachmentCompletedEventArgs> uploadFileChunkCompleted = null; uploadFileChunkCompleted = (s1, e1) => { if (stream.Position > -1 && stream.Position < stream.Length) { chunkSize = Utils.GetCurrentChunkSize(stream, transferChunkSize); double percentage = (stream.Position/file.Size); string fileName = file.Attachment.Filename.Remove(0, file.Attachment.Filename.IndexOf(']') + 1).Trim(); file.Attachment.Filename = String.Format("[{0:#.##%}] {1}", percentage, fileName); fileBytes = new byte[chunkSize]; stream.Read(fileBytes, 0, chunkSize); View.RadProgressBar1.Value = View.RadProgressBar1.Value + chunkSize; cmsWebServiceClient.UploadAttachmentAsync(file.Name, fileBytes, file.Path, true); } else { file.Attachment.Filename = file.Name; cmsWebServiceClient.UploadAttachmentCompleted -= uploadFileChunkCompleted; Attachments.Add(newAttachment); //Close the stream when all files are uploaded stream.Close(); UploadInProgress = false; if (dialogFiles.Count > fileNumber + 1) { fileNumber++; ProccessTransferFiles(fileNumber, dialogFiles[fileNumber], dialogFiles); } mIssue.IssueFiles.Add(newAttachment); RaisePropertyChanged("SelectedAttachmentType"); RaisePropertyChanged("Attachments"); OnCollectionChanged(); //UPLOAD IS COMPLETE - Save Model cmsWebServiceClient.SaveIssueFilesCompleted += ((sender, args) => { EventAggregator.GetEvent<PrismEvents.RefreshIssueRevisionHistoryPrismEvent>().Publish(null); }); cmsWebServiceClient.SaveIssueFilesAsync(new List<IssueFile> {newAttachment}); View.RadProgressBar1.Visibility = Visibility.Collapsed; OnUploadComplete(); } }; cmsWebServiceClient.UploadAttachmentCompleted += uploadFileChunkCompleted; cmsWebServiceClient.UploadAttachmentAsync(file.Name, fileBytes, file.Path, false); } } catch (IOException) { OnErrorOccurred(string.Format("Cannot upload the file '{0}' because it is currently open. Please close the file and try again", dialogFile.Name)); } catch (SecurityException) { OnErrorOccurred(string.Format("Cannot upload the file '{0}' because it is currently open. Please close the file and try again", dialogFile.Name)); } catch (Exception ex) { OnErrorOccurred(ex.Message); } }
private bool TransferFile(FileInfo fileInfo, DocumentVersion documentVersion) { long fileSize = fileInfo.Length; ProgressBarView.ProgressBar.Maximum = fileSize; ProgressBarView.ProgressBar.Minimum = 0; ProgressBarView.ProgressBar.Visibility = Visibility.Visible; ProgressBarView.ProgressBar.Value = 0; CmsWebServiceClient cmsWebServiceClient = new CmsWebServiceClient(Utils.WcfBinding, Utils.WcfEndPoint); try { Stream stream = fileInfo.OpenRead(); if (stream.Length > int.MaxValue) { var errors = Utils.BuildValidationResultFromServerErrors("Upload Failed.", new List<string> { string.Format("File [{0}] can not be bigger than 2GB.", fileInfo.Name) }); if (DialogView != null) { DialogView.ValidationPopup.Show(errors); RaiseTransferFailed(); return false; } mDocumentVersionsControl.ValidationPopup.Show(errors); RaiseTransferFailed(); return false; } int transferChunkSize = Utils.GetTransferChunkSize(stream); CommonUploadFile file = new CommonUploadFile { Name = fileInfo.Name, Size = stream.Length, Path = documentVersion.Path }; if (stream.Position > -1 && stream.Position < stream.Length) { int chunkSize = Utils.GetCurrentChunkSize(stream, transferChunkSize); byte[] fileBytes = new byte[chunkSize]; stream.Read(fileBytes, 0, chunkSize); cmsWebServiceClient.UploadFileCompleted += (s1, e1) => { if (stream.Position > -1 && stream.Position < stream.Length) { chunkSize = Utils.GetCurrentChunkSize(stream, transferChunkSize); fileBytes = new byte[chunkSize]; stream.Read(fileBytes, 0, chunkSize); cmsWebServiceClient.UploadFileAsync(file.Name, fileBytes, CMS.AppSetting.DocumentsRootUrl, file.Path, true); if (DialogView != null) { DialogView.RadProgressBar1.Value = DialogView.RadProgressBar1.Value + chunkSize; } ProgressBarView.ProgressBar.Value = ProgressBarView.ProgressBar.Value + chunkSize; } else { //Close the stream when all files are uploaded stream.Close(); if (e1.Result.EntityResult.WrittenByteArraySize != fileSize) { Dictionary<string, List<ValidationResult>> errors = Utils.BuildValidationResultFromServerErrors("Upload Failed.", new List<string> { "There was a loss of information while attempting to transfer the file. Please try again." }); if (DialogView != null) { DialogView.ValidationPopup.Show(errors); RaiseTransferFailed(); return; } mDocumentVersionsControl.ValidationPopup.Show(errors); RaiseTransferFailed(); return; } RaiseTransferComplete(documentVersion); ProgressBarView.ProgressBar.Visibility = Visibility.Collapsed; } }; cmsWebServiceClient.UploadFileAsync(file.Name, fileBytes, CMS.AppSetting.DocumentsRootUrl, file.Path, false); } } catch (IOException) { OnErrorOccurred(string.Format("Cannot upload the file '{0}' because it is currently open. Please close the file and try again", fileInfo.Name)); return false; } catch (SecurityException) { OnErrorOccurred(string.Format("Cannot upload the file '{0}' because it is currently open. Please close the file and try again", fileInfo.Name)); return false; } catch (Exception ex) { OnErrorOccurred(ex.Message); return false; } return true; }