问题
I have a MVC application that has a Product model and multiple child images in an Image entity, which are uploaded to Azure Blob Storage. This all works great on desktop but will not work with a mobile browser. Just straight MVC, no Web API.
Any ideas why the mobile browsers won't pass the selected image to the controller?
I repeat: this works fine on a desktop with just about any browser... just not mobile.
Model:
public class Image
{
public int Id { get; set; }
public string ImageGuid { get; set; }
public string ImageUrl { get; set; }
[Display(Name ="Image Title")]
public string ImageTitle { get; set; }
[Display(Name ="Image File")]
public byte[] ImageFile { get; set; }
public int ProductId { get; set; }
public virtual Product product { get; set; }
}
View (Create):
<input type="file" id="file" name="file" accept="image/*" />
Controller:
public ActionResult Create([Bind(Include = "Id,ImageGuid,ImageUrl,ProductId,ImageTitle,ImageFile")] Image image, HttpPostedFileBase file)
{
//Yes... I know this might not be the best place for this block...
if (file != null && file.ContentLength != 0)
{
string guid = System.Guid.NewGuid().ToString();
image.ImageGuid = guid;
CloudBlobContainer blobContainer = GetBlobContainer();
//Get reference to the picture blob or create if not exists.
CloudBlockBlob blockBlob = blobContainer.GetBlockBlobReference(guid);
//Save to storage
blockBlob.UploadFromStreamAsync(file.InputStream);
image.ImageUrl = blobContainer.Uri + "/" + image.ImageGuid;
}
if (ModelState.IsValid)
{
db.Images.Add(image);
db.SaveChanges();
return RedirectToAction("Details", "Products", new { id = image.ProductId });
}
ViewBag.ProductId = new SelectList(db.Products, "Id", "ProductName", image.ProductId);
return View(image);
}
来源:https://stackoverflow.com/questions/35493219/mvc-5-application-photo-uploads-work-fine-on-desktop-but-not-on-mobile