How to upload image in ASP.NET MVC 4 using ajax or any other technique without postback?

前端 未结 7 1761
鱼传尺愫
鱼传尺愫 2020-12-05 05:40

I am developing a website in MVC 4, where user fill some information and save it to upload. all the information except image is being saved on server using Javascript, Json

7条回答
  •  暖寄归人
    2020-12-05 06:29

    I also got similar Problem and after getting stuck up for many days finally this link helped me

    Jquery Uploadiy with Progress Bar to be Used with MVC

    and this is how i managed it

    public JsonResult Upload(HttpPostedFileBase file)
    {
        if (Session["myAL"] == null)
        {
            al = new ArrayList();
        }
        else
            al = (ArrayList)Session["myAL"];
    
        var uploadFile = file;
    
            if (uploadFile != null && uploadFile.ContentLength > 0)
            {
                string filePath = Path.Combine(HttpContext.Server.MapPath("~/Content/Uploads"),
                                                   Path.GetFileName(uploadFile.FileName));                    
                al.Add(filePath);
                Session["myAL"] = al;
                uploadFile.SaveAs(filePath);
            }
    
        var percentage = default(float);
    
        if (_totalCount > 0)
        {
            _uploadCount += 1;
            percentage = (_uploadCount / _totalCount) * 100;
        }
    
        return Json(new
        {
            Percentage = percentage
        });
    }
    

    How to Implement Attach More Files in MVC and jquery for FileUploading

提交回复
热议问题