2013-02-22 84 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/)之类的东西来抓取请求并将其与你的相比较。

相关问题