2012-06-13 22 views
1

我正在用xcode在mac os中開發一個phonegap項目。在xcode中,如果我創建一個基於cordova的應用程序,它會自動創建cordova-1.6.0.js。我使用fileupload插件發送svg文件到我的服務器。在fileupload.js我已經寫了警報fileuplaoder功能,如下圖所示ios中的fileupload插件無法正常工作

var FileUploader = function() { 
    alert("gi"); 
} 

此警報的工作,但是當我給下上傳功能的ALER,

FileUploader.prototype.upload = function(server, file, params, fileKey, fileName, mimeType, success, fail, progress) { 
    alert("upload"); 
    this._doUpload('upload', server, file, params, fileKey, fileName, mimeType, success, fail, progress); 
}; 

此警報不工作。我在html頁面中的這個插件的呼叫是,

window.plugins.fileUploader.upload('http:192.168.1.54:8080/downloadFiles', '/Users/User/Library/Application Support/iPhone Simulator/5.0/Applications/408DBBC7-67F7-4E8B-B41C-663CDC0377B5/Documents/image5_1.jpg.txt.svg', {foo: 'bar'}, 'myPhoto', 'image5_1.jpg.txt.svg', 'image/svg', 
               function(result) { 
               console.log('Done: ' + result); 
               }, 
               function(result) { 
               console.log("Error: " + result); 
               }, 
               function(loaded, total) { 
               var percent = 100/total * loaded; 
               console.log('Uploaded ' + percent); 

               } 
               ); 

在fileupload.js中有cordova.addConstructor方法。但在我生成的cordova.1.6.0.js文件中沒有這種方法。我不知道發生了什麼。 pl幫我做這個插件。

+0

大家好,pl幫我解決我上面的問題。我需要答案。 – mmathan

回答

0

我找到了解決方案。 cordova文件api本身有一個上傳和下載選項。它的工作正常。代碼是,

document.addEventListener("deviceready", onDeviceReady, false); 

      // Cordova is ready 
      // 
      function onDeviceReady() { 
       window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); 
      } 

      function gotFS(fileSystem) { 
       fileSystem.root.getFile("image5_2.jpg.svg", {create: true, exclusive: false}, gotFileEntry, fail); 
      } 

      function gotFileEntry(fileEntry) { 
       var localpath=fileEntry.fullPath; 
       uploadPhoto(localpath); 
       //fileEntry.createWriter(gotFileWriter, fail); 
      } 

      function uploadPhoto(imageURI) { 
       alert(imageURI); 
       var options = new FileUploadOptions(); 
       options.fileKey="file"; 
       options.fileName="image5_2.jpg.svg"; 
       options.mimeType="image/svg+xml"; 

       var params = new Object(); 
       params.value1 = "test"; 
       params.value2 = "param"; 

       options.params = params; 

       var ft = new FileTransfer(); 
       ft.upload(imageURI, "http://192.168.1.54:8080/POC/fileUploader", win, fail, options); 
      } 

      function win(r) { 
       console.log("Code = " + r.responseCode); 
       console.log("Response = " + r.response); 
       console.log("Sent = " + r.bytesSent); 
      } 

      function fail(error) { 
       alert("An error has occurred: Code = " + error.code); 
       console.log("upload error source " + error.source); 
       console.log("upload error target " + error.target); 
      } 

pl使用此。