Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET Javascript
Dear Experts,
 
I am doing an ASP.Net project, I need to send some datas and post form data through javascript to webservice....
 
Here is the below code of javascript.
 

 
function startUploading() {
 
   
        // get form data for POSTing
        //var vFD = document.getElementById('form-student-profile').getFormData(); // for FF3
        var vFD = new FormData(document.getElementById('form-student-profile'));
 
        // create XMLHttpRequest object, adding few event listeners, and POSTing our data
        var oXHR = new XMLHttpRequest();
        oXHR.upload.addEventListener('progress', uploadProgress, false);
        oXHR.addEventListener('load', uploadFinish, false);
        oXHR.addEventListener('error', uploadError, false);
        oXHR.addEventListener('abort', uploadAbort, false);
        oXHR.open('POST','../../Webservice.asmx/Img_Upload');
        oXHR.send(vFD);
 
       // set inner timer
        oTimer = setInterval(doInnerUpdates, 300);
    
}
 
 
Here is the code in WebService
 
 
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public void Img_Upload()
        {
            HttpContext postedContext = HttpContext.Current;
            HttpPostedFile file = postedContext.Request.Files[0];
 
            string name = file.FileName;
            byte[] binaryWriteArray = new
            byte[file.InputStream.Length];
            file.InputStream.Read(binaryWriteArray, 0,
            (int)file.InputStream.Length);
 
            FileInfo file_Info = new FileInfo(file.FileName);
            string ext = file_Info.Extension;
 

            string file_Name = Guid.NewGuid().ToString() + "_" + DateTime.Now.Day + DateTime.Now.Month + DateTime.Now.Year + ext;
 
            FileStream objfilestream = new FileStream(Server.MapPath("img//profilePIC//" + file_Name), FileMode.Create, FileAccess.ReadWrite);
            objfilestream.Write(binaryWriteArray, 0,
            binaryWriteArray.Length);
            objfilestream.Close();
 
            SetVal.Profile_PIC = file_Name;
 
          
        }
 
 
Here i can get the image file sucessfully, and i need to get some more string datas from that javascript function to this Webservice function....
 
Is there any method to get the file and datas ..??
 
please Help me..
 
Thanks and regards,
 
Dileep
Posted 15-Apr-13 2:31am
dilzz1.2K
Comments
Prasad Khandekar at 15-Apr-13 16:06pm
   
Hello,
 
Please read about multi-part form data. Please have a look at this (https://github.com/blueimp/jQuery-File-Upload/wiki/Options) JQuery plugin or http://fineuploader.com/
 
Regards,

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

I found your question looking for some help myself with something very similar.
You can easily add some more values to your POST.
 
In you javascript you can simply add:
vFD.append("key1", "somevalue");
 
and in the codebehind:
string value = postedContext.Request["key1"];
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 319
1 Kornfeld Eliyahu Peter 265
2 Maciej Los 249
3 BillWoodruff 205
4 Sergey Alexandrovich Kryukov 175
0 OriginalGriff 5,107
1 DamithSL 4,157
2 Maciej Los 3,595
3 Kornfeld Eliyahu Peter 3,450
4 Sergey Alexandrovich Kryukov 2,741


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 27 May 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100