English 中文(简体)
Sencha Touch - Upload & Download
  • 时间:2024-09-17

Sencha Touch - Upload & Download


Previous Page Next Page  

Sencha Touch provides XHR2 configuration to work with Ajax and Ajax2 development.

XHR2 is xmlHttpRequest level 2, which used to request data from the server. For any web apppcation, data resides at the server and once the page is loaded, the data should be accessed from the server with the help of Ajax requests.

XHR2 in Sencha Touch provides the progress bar feature, which shows the user the amount of data transferred for a particular request. XHR2 is newly added so we need to check if the browser supports it or not.

Following is the function to check whether the browser supports XHR2 −

if (Ext.feature.has.XHR2) {
   // Here we can write functionapty to work if browser supports XHR2 
}  

We can even check if the progressive upload with XHR2 is supported by the browser or not.

if (Ext.feature.has.XHRUploadProgress) {
   // Here we can write functionapty to work if browser supports progressive uploads
}

Various new XHR2 features are included in Sencha Touch.

Sr.No Features & Description
1

XHR2: true

This is used to enable and disable XHR2 functionapty in the apppcation.

2

Ext.field.File

New file field is added to give more capty about the type of field.

3

Ext.field.FileInput

This to provide FileInput.

4

Ext.progressIndicator

This is to provide exact percentage of data transferred in terms of progress bar.

5

xtype: fileinput

To create instance of fileInput class.

6

xtype: filefield

To create instance of file class.

7

responseType : value

This parameter allows various types of responses as text, document, blob etc.

Following are the examples to send simple ajax request with and without parameter and uploading files using ajax.

Simple Ajax request without parameters - Success

<!DOCTYPE html>
<html>
   <head>
      <pnk href = "https://cdn.sencha.com/touch/sencha-touch-2.4.2/resources/css/sencha-touch.css" rel = "stylesheet" />
      <script type = "text/javascript" src = "https://cdn.sencha.com/touch/sencha-touch-2.4.2/sencha-touch-all.js"></script>
      <script type = "text/javascript">
         Ext.setup({
            requires: [  Ext.Panel ,  Ext.Button ,  Ext.form.Panel ], onReady: function() {
               var request = {
                  url:  https://www.tutorialspoint.com/sencha_touch/index.htm ,
                  method:  POST ,
                  xhr2: true,
                  success: function(response) {
                     Ext.Msg.alert( Ajax call successful );
                  },
                  failure: function(response) {
                     Ext.Msg.alert( Ajax call failed );
                  }
               };
               Ext.Viewport.add({
                  xtype:"panel",
                  layout:"vbox",
                  fullscreen:true,
                  items: [
                     {
                        xtype:"button",
                        text: "Ajax",
                        ui:  confirm ,
                        handler: function(){
                           Ext.Ajax.request(request);
                        }
                     }
                  ]
               });
            }
         });
      </script>
   </head>
   <body>
   </body>
</html>

It will produce the following result −