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

前端 未结 7 1758
鱼传尺愫
鱼传尺愫 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:34

    
    
     if (file != null && file.ContentLength > 0)
                    {
                        string filename = Path.GetFileName(file.FileName);
                        string imgpath = Path.Combine(Server.MapPath("~/Img/"), filename);
                        file.SaveAs(imgpath);
                        student.photo = imgpath;
                    }
    
    function readURL(input)
        {
            if (input.files && input.files[0]) {
                var reader = new FileReader();
    
                reader.onload = function (e) {
                    $('#imgUser')
                        .attr('src', e.target.result)
                        .width(150)
                        .height(200);
                };
    
                reader.readAsDataURL(input.files[0]);
            }
        }
    

提交回复
热议问题