/// <summary> /// Read the non-file contents as form data. /// </summary> /// <returns></returns> public override async Task ExecutePostProcessingAsync() { // Find instances of non-file HttpContents and read them asynchronously // to get the string content and then add that as form data for (int index = 0; index < Contents.Count; index++) { if (_isFormData[index]) { HttpContent formContent = Contents[index]; // Extract name from Content-Disposition header. We know from earlier that the header is present. ContentDispositionHeaderValue contentDisposition = formContent.Headers.ContentDisposition; string formFieldName = UnquoteToken(contentDisposition.Name) ?? String.Empty; // Read the contents as string data and add to form data var formFieldValue = await formContent.ReadAsAsync <StationImageUploadModel>(); stationImageUploadModel = formFieldValue; //FormData.Add(formFieldName, formFieldValue); } else { fileContents.Add(Contents[index]); } } }
private async Task <List <StationImageUploadModel> > UploadBlobsToAzure(IList <HttpContent> files, StationImageUploadModel uploadedModel) { // NOTE: FileData is a property of MultipartFileStreamProvider and is a list of multipart // files that have been uploaded and saved to disk in the Path.GetTempPath() location. foreach (var fileData in files) { if (!string.IsNullOrEmpty(fileData.Headers.ContentDisposition.FileName)) { string mediaType = fileData.Headers.ContentType.MediaType.ToString(); //If the file an image media type if (System.Text.RegularExpressions.Regex.IsMatch(mediaType, "image/\\S+")) { // Sometimes the filename has a leading and trailing double-quote character // when uploaded, so we trim it; otherwise, we get an illegal character exception var fileName = Path.GetFileName(fileData.Headers.ContentDisposition.FileName.Trim('"')); var path = HttpRuntime.AppDomainAppPath; string directoryName = Path.Combine(path, "StationImage"); if (!Directory.Exists(directoryName)) { Directory.CreateDirectory(@directoryName); } string filePath = Path.Combine(directoryName, fileName); //Deletion exists file if (File.Exists(filePath)) { File.Delete(filePath); } // Retrieve reference to a blob var blobContainer = BlobHelper.GetBlobContainer(); var blob = blobContainer.GetBlockBlobReference(fileName); bool blobExists = blob.Exists(); //if is doesn't exist, then add it. if (!blobExists) { // Set the blob content type blob.Properties.ContentType = fileData.Headers.ContentType.MediaType; Stream input = await fileData.ReadAsStreamAsync(); //Directory.CreateDirectory(@directoryName); using (Stream file = File.OpenWrite(filePath)) { input.CopyTo(file); //close file file.Close(); } // Upload file into blob storage, basically copying it from local disk into Azure using (var fs = File.OpenRead(filePath)) { long fileSizeInKB = (long)(fs.Length / 1024); //If the image is greater than 1 MB don't save it if (fileSizeInKB > 1001) { continue; } blob.UploadFromStream(fs); } // Delete local file from disk File.Delete(filePath); } // Create blob upload model with properties from blob info var stationImageUploadModel = new StationImageUploadModel { FileName = blob.Name, FileUrl = blob.Uri.AbsoluteUri, FileSizeInBytes = blob.Properties.Length, StationID = uploadedModel.StationID, User = uploadedModel.User, Primary = uploadedModel.Primary, ImageTypeID = uploadedModel.ImageTypeID, Description = uploadedModel.Description, PhysHabYear = uploadedModel.PhysHabYear }; // Add uploaded blob to the list Uploads.Add(stationImageUploadModel); } } } return(Uploads); }
public async Task <List <StationImageUploadModel> > UploadBlobs(IList <HttpContent> files, StationImageUploadModel model) { Uploads = new List <StationImageUploadModel>(); Uploads = await UploadBlobsToAzure(files, model); //Task taskUpload = await UploadBlobsToAzure(files, model) // .ContinueWith(task => // { // Task<List<StationImageUploadModel>> uploadList = null; // if (task.IsFaulted || task.IsCanceled) // { // throw task.Exception; // } // if (task.Status == TaskStatus.RanToCompletion) // { // var result = task.Result; // Uploads = result.ToList(); // } // return Uploads; // }); //taskUpload.Wait(); if (!SaveImage(Uploads)) { return(null); } return(Uploads); }