Ajax File upload

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ramg80
    New Member
    • Sep 2007
    • 5

    Ajax File upload

    Hi,

    I m using ajax to upload a file. I m using servlets (MultipartFormD ataRequest) to upload the file.

    My script contains the form data and the file. I m able to get the values of the form data.

    Can any one please tell me what i m missing here.

    [CODE=javascript]var boundaryString = 'AaB03x';
    var boundary = boundaryString;

    var requestBody = setBody(documen t.getElementByI d('clientId'),b oundary);
    requestBody += setBody(documen t.getElementByI d('feed_format' ),boundary);
    requestBody += setBody(documen t.getElementByI d('delimiter'), boundary);
    requestBody += setBody(documen t.getElementByI d('qualifier'), boundary);
    requestBody += setBody(documen t.getElementByI d('applicationI d'),boundary);
    requestBody += setBody(documen t.getElementByI d('applicationN ame'),boundary) ;
    //requestBody += setBody(documen t.getElementByI d('upload'),bou ndary);
    requestBody += setFile(documen t.getElementByI d('upload'),bou ndary);
    requestBody += "--" + boundary;

    function setBody(element , boundary){
    alert(element.n ame + ", " + element.value);
    var requestBody = "--" + boundary + "\r\n";
    requestBody += "Content-Disposition: form-data; name=\"" + element.name + "\"" + "\r\n\r\n";
    requestBody += element.value + "\r\n";
    return requestBody;
    }

    function setFile(element , boundary){
    alert(element.n ame + ", " + element.value);
    var requestBody = "--" + boundary + "\r\n";
    requestBody += "Content-Disposition: form-data; name=\"" + element.name + "\"" + "\r\n\r\n";
    //requestBody += element.value + "\r\n\r\r";
    requestBody += "filename="+ele ment.value+"\r\ n"+"Content-Type: text/plain\r\n";
    return requestBody;
    }
    [/CODE]
    The above sets the boundary for the form fields, How do i set content type and disposition elements for file element.



    Thanks & Regards
    ram
    Last edited by gits; Sep 12 '07, 06:45 AM. Reason: added code tags
  • ramg80
    New Member
    • Sep 2007
    • 5

    #2
    Originally posted by ramg80
    Hi,

    I m using ajax to upload a file. I m using servlets (MultipartFormD ataRequest) to upload the file.

    My script contains the form data and the file. I m able to get the values of the form data.

    Can any one please tell me what i m missing here.

    [CODE=javascript]var boundaryString = 'AaB03x';
    var boundary = boundaryString;

    var requestBody = setBody(documen t.getElementByI d('clientId'),b oundary);
    requestBody += setBody(documen t.getElementByI d('feed_format' ),boundary);
    requestBody += setBody(documen t.getElementByI d('delimiter'), boundary);
    requestBody += setBody(documen t.getElementByI d('qualifier'), boundary);
    requestBody += setBody(documen t.getElementByI d('applicationI d'),boundary);
    requestBody += setBody(documen t.getElementByI d('applicationN ame'),boundary) ;
    //requestBody += setBody(documen t.getElementByI d('upload'),bou ndary);
    requestBody += setFile(documen t.getElementByI d('upload'),bou ndary);
    requestBody += "--" + boundary;

    function setBody(element , boundary){
    alert(element.n ame + ", " + element.value);
    var requestBody = "--" + boundary + "\r\n";
    requestBody += "Content-Disposition: form-data; name=\"" + element.name + "\"" + "\r\n\r\n";
    requestBody += element.value + "\r\n";
    return requestBody;
    }

    function setFile(element , boundary){
    alert(element.n ame + ", " + element.value);
    var requestBody = "--" + boundary + "\r\n";
    requestBody += "Content-Disposition: form-data; name=\"" + element.name + "\"" + "\r\n\r\n";
    //requestBody += element.value + "\r\n\r\r";
    requestBody += "filename="+ele ment.value+"\r\ n"+"Content-Type: text/plain\r\n";
    return requestBody;
    }
    [/CODE]
    The above sets the boundary for the form fields, How do i set content type and disposition elements for file element.



    Thanks & Regards
    ram

    Hi,

    I m able to upload the file to my server location, But the problem is it stroring the details.


    Could any one help me in this regards.

    The below is the script which is used to organize the request header.
    [code=javascript]
    function setFile(element , boundary){
    alert(element.n ame + ", " + element.value);
    var requestBody = "--" + boundary + "\r\n";
    requestBody += "Content-Disposition: form-data; name=\"" + element.name + "\";filename=\" " + element.value +"\""+ "\r\n\r\n";
    requestBody += "Content-Type: text/plain\r\n";
    return requestBody;
    }
    [/code]

    Result is the file is getting uploaded but failed to save the content.. The content in thew file is Content-Type: text/plain



    Please reply .

    Thanks & regards
    Ram
    Last edited by pbmods; Sep 12 '07, 11:04 PM. Reason: Added CODE tags.

    Comment

    • acoder
      Recognized Expert MVP
      • Nov 2006
      • 16032

      #3
      See this thread. Use an iframe to upload a file without reloading the whole page.

      Comment

      • pbmods
        Recognized Expert Expert
        • Apr 2007
        • 5821

        #4
        Heya, Ram.

        Please use CODE tags when posting source code:

        [CODE=javas cript]
        JavaScript code goes here.
        [/CODE]

        Comment

        • ramg80
          New Member
          • Sep 2007
          • 5

          #5
          Originally posted by pbmods
          Heya, Ram.

          Please use CODE tags when posting source code:

          [CODE=javascript]
          JavaScript code goes here.
          [/CODE]
          pbmods,

          I m new to the community discussions.., Sorry N thanks..

          Comment

          • ramg80
            New Member
            • Sep 2007
            • 5

            #6
            Originally posted by acoder
            See this thread. Use an iframe to upload a file without reloading the whole page.
            Thanks a lot Buddy. I got the solution

            Comment

            • acoder
              Recognized Expert MVP
              • Nov 2006
              • 16032

              #7
              Glad to hear you got it working.

              Comment

              Working...