MVC 5 Application / Photo uploads work fine on desktop but not on mobile

纵饮孤独 提交于 2019-12-25 11:56:26

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!