public async Task<ActionResult> Create( [Bind(Include = "Title,Price,Description,Category,Phone")] Ad ad, HttpPostedFileBase imageFile) { CloudBlockBlob imageBlob = null; // A production app would implement more robust input validation. // For example, validate that the image file size is not too large. if (ModelState.IsValid) { if (imageFile != null && imageFile.ContentLength != 0) { imageBlob = await UploadAndSaveBlobAsync(imageFile); ad.ImageURL = imageBlob.Uri.ToString(); } ad.PostedDate = DateTime.Now; db.Ads.Add(ad); await db.SaveChangesAsync(); Trace.TraceInformation("Created AdId {0} in database", ad.AdId); if (imageBlob != null) { BlobInformation blobInfo = new BlobInformation() { AdId = ad.AdId, BlobUri = new Uri(ad.ImageURL) }; var queueMessage = new CloudQueueMessage(JsonConvert.SerializeObject(blobInfo)); await thumbnailRequestQueue.AddMessageAsync(queueMessage); Trace.TraceInformation("Created queue message for AdId {0}", ad.AdId); } return RedirectToAction("Index"); } return View(ad); }
public async Task<ActionResult> Edit( [Bind(Include = "AdId,Title,Price,Description,ImageURL,ThumbnailURL,PostedDate,Category,Phone")] Ad ad, HttpPostedFileBase imageFile) { CloudBlockBlob imageBlob = null; if (ModelState.IsValid) { if (imageFile != null && imageFile.ContentLength != 0) { // User is changing the image -- delete the existing // image blobs and then upload and save a new one. await DeleteAdBlobsAsync(ad); imageBlob = await UploadAndSaveBlobAsync(imageFile); ad.ImageURL = imageBlob.Uri.ToString(); } db.Entry(ad).State = EntityState.Modified; await db.SaveChangesAsync(); Trace.TraceInformation("Updated AdId {0} in database", ad.AdId); if (imageBlob != null) { BlobInformation blobInfo = new BlobInformation() { AdId = ad.AdId, BlobUri = new Uri(ad.ImageURL) }; var queueMessage = new CloudQueueMessage(JsonConvert.SerializeObject(blobInfo)); await thumbnailRequestQueue.AddMessageAsync(queueMessage); Trace.TraceInformation("Created queue message for AdId {0}", ad.AdId); } return RedirectToAction("Index"); } return View(ad); }