Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
I made loads images from client to server I wrote the following code. MVC3:
1.view :
<input id="btnUpload" type="button" value="Upload image" />
- code jquery :
JavaScript
$('#btnUpload').click(function () {
     alert('started');
     var file = $('#txtfile').image;

     $.ajax({
         url: '/Manager/InsertProduct1',
         type: 'POST',
         dataType: 'json',
         data:file,
         contentType: 'application/json; charset=utf-8',
         success: function (data) {
             var d = data;
             alert(d);

         },
         error: function (er) {
             alert(er);
         }

     });
 });

2. code controller :
JavaScript
<pre lang="Javascript">

 <pre lang="c#"> [HttpPost]
        public ActionResult InsertProduct1(HttpPostedFileBase file)
        {

            string fileName = file.FileName;
            file.SaveAs(Server.MapPath("~/Content/Images"));
            return Json("Complete!");
        }


I do the job fails, I need help?
Posted

 
Share this answer
 
Comments
MinhAnhdhsbshjs 1-Oct-13 21:29pm    
Thank, You cannot help me, do not use plugin?
i want to use ajax jquery simple
F-ES Sitecore 14-Oct-15 10:03am    
Google "ajax upload jquery" and you'll find lots of examples, please do your own basic research before asking a question as this question is asked daily. You can only do this if the browser supports html5's file api, otherwise you'll have to use a plug-in or a non-ajax upload. jQuery isn't magic, it can't do things js can't and js doesn't have access to the local file system so can't do uploads.
Sampath Kumar Sathiya 3-Oct-13 3:59am    
Oh is it so?
MVC View:
C#
<input type="file" id="uploadEditorImage" />

Javascript Code Here
C#
$("#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);
        }

    });
});

MVC Controller
C#
if (System.Web.HttpContext.Current.Request.Files.AllKeys.Any())
        {
            var pic = System.Web.HttpContext.Current.Request.Files["HelpSectionImages"];
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900