Click here to Skip to main content
15,124,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.


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

C#
[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
Comments
Prasad Khandekar 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

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:
JavaScript
vFD.append("key1", "somevalue");


and in the codebehind:
C#
string value = postedContext.Request["key1"];
   

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