2013-02-22 25 views
3

我見過很多示例和論壇,但對我而言還不清楚。 我想用手機拍照,然後將其發送到WebService。
在WebService中,我有一個功能UploadImage,它將圖像保存在網站中。這個函數的參數是:從移動Web應用程序(PhoneGap和JavaScript)上傳圖像到SOAP Web服務返回狀態0

<UploadImage xmlns="http://*********"> 
    <urlSite>http://********</urlSite> 
    <iListName>{*************}</iListName> 
    <iUpdateMode>*******</iUpdateMode> 
    <iMetaData><Fields><Field Name=」Title」 >Title of the Image</Field></Fields></iMetaData> 
    <iAttachments><Attachments><Attachment Name=」Name of the file」>file in the format Base64String</Attachment></Attachments></iAttachments> 
    <iOverWrite>****</iOverWrite> 
</UploadImage> 

POST請求:

function sendImage() 
{ 
    var wsUrl = "*******?op=UploadImage"; 
    var soapRequest ='<?xml version="1.0" encoding="utf-8"?> \ 
     <soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" \ 
     xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> \ 
    <soap:Body> \ 
    <UploadImage xmlns="**********"> \ 
     <urlSite>**********</urlSite> \ 
     <iListName>{******}</iListName> \ 
     <iUpdateMode>********</iUpdateMode> \ 
     <iMetaData><Fields><Field Name=」Title」 >Title of the Image</Field></Fields></iMetaData> 
     <iAttachments><Attachments><Attachment Name=」Name of the file」>file in the format Base64String</Attachment></Attachments></iAttachments> \ 
     <iOverWrite>*****</iOverWrite> \ 
    </UploadImage> \ 
    </soap:Body> \ 
</soap:Envelope>'; 
    var xmlhttp = createXMLHttpRequest(); 


xmlhttp.open("POST", wsUrl, true,"username","password"); 

xmlhttp.setRequestHeader ("Post", "********"); 
xmlhttp.setRequestHeader ("Host", "*****"); 
xmlhttp.setRequestHeader ("Content-Type", "text/xml; charset=utf-8"); 
xmlhttp.setRequestHeader("Content-Length", soapRequest.length); 
xmlhttp.setRequestHeader ("SOAPAction", "******"); 


xmlhttp.onerror = function(e) { 
    alert("Error ocurred. Error = " + e.message); 
} 

xmlhttp.ontimeout = function(e) { 
    alert("Timeout error!"); 
} 

xmlhttp.onreadystatechange = function() { 
    alert(xmlhttp.readyState); 

    if (xmlhttp.readyState==4) 
    { 
     alert(xmlhttp.responseText); 
     alert(xmlhttp.status); 
     // if "OK" 
     if (xmlhttp.status==200 || xmlhttp.status==0) 
     { 
      alert(xmlhttp.responseXML); 
     //[Get xmlhttp.responseXML.xml and do something with it] 

     } 
     else 
     { 
     //[Get xmlhttp.responseXML.xml and do something with it in the case of an error] 
     } 
    } 
} 


xmlhttp.send(soapRequest); 

}

狀態T =返回值爲0
在標籤<iAttachments><Attachments><Attachment Name=」Name of the file」>file in the format Base64String</Attachment></Attachments></iAttachments>我應該發送一個文件一個Base64格式,但我不知道如何將這種格式的文件放在這個標籤中,所以我發送一個字符串,也許這是問題的原因!

我試圖與jquery.ajax將它張貼:

function sendReq() 
{ var wsUrl = "*******"; 
    var soapRequest ='<?xml version="1.0" encoding="utf-8"?>'+ 
     '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'+ 
    '<soap:Body>'+ 
    '<UpdateImage xmlns="*******">'+ 
     '<urlSite>*********</urlSite>'+ 
     '<iListName>{*****}</iListName>'+ 
     '<iUpdateMode>****</iUpdateMode>'+ 
     '<iMetaData><Fields><Field Name="Title" >'+my_var1+'</Field></Fields></iMetaData>'+ 
     '<iAttachments><Attachments><Attachment Name="Nom du fichier 1">'+my_var2+'</Attachment></Attachments></iAttachments>'+ 
     '<iOverWrite>*****</iOverWrite>'+ 
    '</UpdateImage>'+ 
    '</soap:Body>'+ 
'</soap:Envelope>'; 
    $.ajax({ 
        type: "POST", 
        url: wsUrl, 
        contentType: "text/xml; charset=utf-8", 
        dataType: "xml", 
        data: soapRequest, 
        beforeSend: function (xhr){ 
     xhr.setRequestHeader('Authorization', make_base_auth('username', 'password')); 
     xhr.setRequestHeader("SOAPAction", "****/UpdateImage"); 
    },     
    contentType: "text/xml; charset=utf-8", 

       success: processSuccess, 
       error: processError 
      }); 
return false; 

}

結果爲:req.responseText=undefinedstatus=error

回答

0

我會Base64編碼,與一些在線工具的文件(例如http://www.opinionatedgeek.com/dotnet/tools/base64encode/)並將字符串硬編碼到您的xml中。看看這是否首先檢查你的代碼的其餘部分是否正常。

如果這樣的作品,然後檢查瞭如何爲Base64編碼的文件在發送前這個問題: Get Base64 encode file-data from Input Form

而且 - 調試代理是在這種情況下非常有用,所以你可以看到正在發送的實際要求。如果你有一些已經可以使用這項服務的東西,你可以使用諸如提琴手(http://fiddler2.com/)之類的東西來抓取請求並將其與你的相比較。

相關問題