question

$$anonymous$$ avatar image
0 Likes"
$$anonymous$$ Suspended asked

[JavaScript] UploadSiteHostedPictures with upload problem

Hi ! I'm trying to use **UploadSiteHostedPictures** with an upload in **JavaScript** and I encounter some problems... I work with **node-webkit** (so the navigator is always Chromium) and the **eBay's sandbox**. I tried two methods : - With HTML5 **FormData** and **Blob/File** objects - With a **String** of data representing a **form-data/multipart** request I make the XHR with jQuery, so I've something like this : var req = $.ajax({ // A modified XHR to follow the progression of the upload xhr : me.createXhr, url : ' https://api.sandbox.ebay.com/ws/api.dll', async : true, cache : false, processData : false, type : 'POST', timeout : 30000, data : formDataOrString, // The content-type depends on the data (FormData or String with boundary) contentType : 'multipart/form-data', // +'; boundary="---16876546856268268552"' headers : { 'X-EBAY-API-CALL-NAME': 'UploadSiteHostedPictures', 'X-EBAY-API-COMPATIBILITY-LEVEL': '883', 'X-EBAY-API-DETAIL-LEVEL': '0', 'X-EBAY-API-SITEID': '71', 'GLOBAL-ID': 'EBAY-FR', 'X-EBAY-API-DEV-NAME': dev-id, 'X-EBAY-API-APP-NAME': app-id, 'X-EBAY-API-CERT-NAME': cert-id }, // It's cross-domain, the header's value of "Origin" is "file://" crossDomain : true }); With the FormData method : # HTTP request : (the data aren't visible) ------WebKitFormBoundaryTHQBGC8lLazYFsYq Content-Disposition: form-data; name="XML_Request"; filename="blob" Content-Type: text/xml ------WebKitFormBoundaryTHQBGC8lLazYFsYq Content-Disposition: form-data; name="File_Request"; filename="PHOTO dindon-5.jpg" Content-Type: image/jpeg ------WebKitFormBoundaryTHQBGC8lLazYFsYq-- # Result : The server receives the data and tells me : XML parsing error : "; nested exception is: org.xml.sax.SAXParseException: An invalid XML character (Unicode: 0x0) was found in the element content of the document."." The String method with manual boundaries : # HTTP request : (I've indented the XML code for visibility) ------1408216539193 Content-Disposition: form-data; name="XML Payload"; filename="XML_Request" Content-Type: text/xml; charset=utf-8 {TOKEN} dindon ------1408216539193 Content-Disposition: form-data; name="dindon"; filename="dindon.jpg" Content-Type: image/jpeg Content-Transfer-Encoding: binary ÿØÿàN™¦mJ[...]ø»VVVL¸ŸÿÙ ------1408216539193-- # Result : The request upload stops at ~25% and throws the jQuery error : POST https://api.sandbox.ebay.com/ws/api.dll net::ERR_CONNECTION_RESET So I am looking for some good advices. Sorry for my english... And have a nice day ! [1]: /storage/temp/ 219-formdata_xml_response.txt
uploadsitehostedpicturesjavascriptuploadtrading jquery imageupload
10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

1 Answer

· Write an Answer
$$anonymous$$ avatar image
0 Likes"
$$anonymous$$ Suspended answered
To everyone encounters the same problem, I've found the (very ultra mega simple) solution. > Use FormData like this : var fd = new FormData(), filePath = 'C:\\monDindon.jpeg', xmlStr = ' '; fd.append('XML_Request', xmlStr); /* * The File constructor isn't available in standard navigators for security reasons * But Node-Webkit allows us to access the constructor */ fd.append('File_Request', new File( filePath, 'monDindon.jpeg' )); > Then send the FormData like this : (without jQuery) var xhr = new XMLHttpRequest(); xhr.open('POST', ' https://api.sandbox.ebay.com/ws/api.dll', true); xhr.setRequestHeader('X-EBAY-API-COMPATIBILITY-LEVEL', '887'); xhr.setRequestHeader('X-EBAY-API-DETAIL-LEVEL', '0'); xhr.setRequestHeader('X-EBAY-API-DEV-NAME', 'xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'); xhr.setRequestHeader('X-EBAY-API-APP-NAME', 'xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'); xhr.setRequestHeader('X-EBAY-API-CERT-NAME', 'xxxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx'); xhr.setRequestHeader('X-EBAY-API-CALL-NAME', 'UploadSiteHostedPictures'); xhr.setRequestHeader('X-EBAY-API-SITEID', '71'); xhr.setRequestHeader('GLOBAL-ID', 'EBAY-FR'); xhr.onload = function() { console.log( xhr.responseXML ); }; xhr.send( fd ); > Or like this : (with jQuery) $.ajax({ url : ' https://api.sandbox.ebay.com/ws/api.dll', async : true, cache : false, type : 'POST', // Our FormData data : fd, // Don't process the data processData : false, // Don't put a content-type contentType : false, headers : { 'X-EBAY-API-COMPATIBILITY-LEVEL': '887', 'X-EBAY-API-DETAIL-LEVEL': '0', 'X-EBAY-API-DEV-NAME': '70ed7862-xxxx-xxxx-xxxx-b7ce22018fba', 'X-EBAY-API-APP-NAME': 'Niet48e61-xxxx-xxxx-xxxx-51deefa21b9', 'X-EBAY-API-CERT-NAME': 'ee5519d4-xxxx-xxxx-xxxx-6ea44087f4e6', 'X-EBAY-API-CALL-NAME': 'UploadSiteHostedPictures', 'X-EBAY-API-SITEID': '71', 'GLOBAL-ID': 'EBAY-FR' }, crossDomain : true, success : function(xml, status, jqXHR) { console.log( xml ); }, error : function(jqXHR, status, errorThrown) { console.error( errorThrown ); } }); I join a complete HTML5 example, working on Node-Webkit et Google Chrome (not sure for others). [link text][1] I hope this will be useful for someone. Have a nice day. [1]: /storage/temp/223-example-uploadsitehostedpictures.zip

10 |600 characters needed characters left characters exceeded

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.