1. #21
    Ext User
    Join Date
    Feb 2009
    Posts
    3
    Vote Rating
    0
    beto.ch is on a distinguished road

      0  

    Default


    i've a servlet to process the upload (spring), but how can i access to servlet response to change the contentType to text/html?

  2. #22

  3. #23
    Ext User
    Join Date
    Jul 2008
    Posts
    8
    Vote Rating
    0
    ypcheng is on a distinguished road

      0  

    Wink tried and worked

    tried and worked


    after several hours of try and error, i got an solution that works in both ie and ff.

    in your upload servelet:

    response.setContentType("text/html");
    response.getWriter().write("{success:true,path:'"+actualFilePath+"'}");

    then in your js

    form.getForm().submit({
    url: '/upload',
    waitMsg: 'Uploading your file...',
    success: function(fp, o) {
    alert(o.result.path)
    }})

  4. #24
    Sencha - Ext JS Dev Team Animal's Avatar
    Join Date
    Mar 2007
    Location
    Notts/Redwood City
    Posts
    30,545
    Vote Rating
    64
    Animal is a jewel in the rough Animal is a jewel in the rough Animal is a jewel in the rough

      0  

    Default


    Several hours instead of just reading the API docs?

    http://extjs.com/deploy/dev/docs/?cl...form.BasicForm

    Quote Originally Posted by TFM
    File Uploads

    File uploads are not performed using Ajax submission, that is they are not performed using XMLHttpRequests. Instead the form is submitted in the standard manner with the DOM <form> element temporarily modified to have its target set to refer to a dynamically generated, hidden <iframe> which is inserted into the document but removed after the return data has been gathered.

    The server response is parsed by the browser to create the document for the IFRAME. If the server is using JSON to send the return object, then the Content-Type header must be set to "text/html" in order to tell the browser to insert the text unchanged into the document body.

  5. #25
    Sencha User
    Join Date
    Oct 2009
    Location
    Cambridge, UK
    Posts
    164
    Vote Rating
    0
    Uberdude is on a distinguished road

      0  

    Default


    A problem arrises if your json response contains html tags:

    {"response":{"message":"Unable to <b>parse<\/b> CSV."},"success":false}

    Note the escaped forawrd slash '\/' in the Javascript string literal. This string is made with org.json.JSONObject#toString and is the correct string that when eval-ed gives you the correct js object (when using normal Ajax instead of file upload works fine). The problem is once written into the dynamic iframe with the content type of text/html (which avoids the <pre> tags you get with text/plain) the browser realisess this isn't valid html and patches it up so that when you extract it with r.responseText = doc.body.innerHTML; in function cb in Ext.data.Connection.doFormUpload) you end up with:

    {"response":{"message":"Unable to <b>parse&lt;\/b&gt; CSV."},"success":false}</b>

    When this gets eval-ed Ext expodes (in Ext.util.JSON.decode) with "unterminated regular expression literal" due to the forward slash in the '</b>' the browser inserted on the end of of our text to make it valid html.

  6. #26
    Sencha - Community Support Team Condor's Avatar
    Join Date
    Mar 2007
    Location
    The Netherlands
    Posts
    24,246
    Vote Rating
    97
    Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of Condor has much to be proud of

      0  

    Default


    Ext also supports sending the JSON back in a textarea, e.g.
    Code:
    <html><body><textarea>{"response":{"message":"Unable to <b>parse</b> CSV."},"success":false}</textarea></body></html>
    No escaping required as long as your JSON doesn't contain '</textarea>'.

  7. #27
    Sencha User
    Join Date
    Oct 2009
    Location
    Cambridge, UK
    Posts
    164
    Vote Rating
    0
    Uberdude is on a distinguished road

      0  

    Default


    Thanks for the quick reply Condor.

    After writing my post I checked the Ext 3 docs (I'm on Ext 2.2.1) and saw that it does now mention escaping html.

    The textarea fix is nice (and I'd always html escape contents to be safe, as Ext gets the .value rather than innerHtml you get right value), but not in Ext 2 . Another reason to upgrade!

  8. #28
    Sencha User Surinder singh's Avatar
    Join Date
    May 2009
    Location
    India
    Posts
    39
    Vote Rating
    0
    Surinder singh is on a distinguished road

      0  

    Lightbulb solved!

    solved!


    Use the following header when u are sending the response from the server.

    header('Content-Type: text/html'); //php

  9. #29
    Sencha Premium Member
    Join Date
    Feb 2011
    Posts
    3
    Vote Rating
    0
    ezzygemini is on a distinguished road

      0  

    Default


    anyone found a solution?

  10. #30
    Sencha Premium Member
    Join Date
    Feb 2011
    Posts
    3
    Vote Rating
    0
    ezzygemini is on a distinguished road

      0  

    Default


    Apparently Chrome is doing this too and I found the solution:
    I switched the repsonse Content-Type on my JSON file to "text/json" instead of "application/json" (only when using the POST method).

    It now works fine on my app.My guess is that is a security feature on some browsers.