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

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

    HTML Code

    <input type="file"  id="uploadEditorImage"  />
    

    Javascript Code

    $("#uploadEditorImage").change(function () {
        var data = new FormData();
        var files = $("#uploadEditorImage").get(0).files;
        if (files.length > 0) {
            data.append("HelpSectionImages", files[0]);
        }
        $.ajax({
            url: resolveUrl("~/Admin/HelpSection/AddTextEditorImage/"),
            type:"POST",
            processData: false,
            contentType: false,
            data: data,
            success: function (response) {
               //code after success
    
            },
            error: function (er) {
                alert(er);
            }
    
        });
    });
    

    Code in MVC controller

    if (System.Web.HttpContext.Current.Request.Files.AllKeys.Any())
            {
                var pic = System.Web.HttpContext.Current.Request.Files["HelpSectionImages"];
            }
    
    0 讨论(0)
提交回复
热议问题