Anthony Musgrove 195 Posted May 24, 2020 Share Posted May 24, 2020 Hi all - I am having an issue that is consuming most of my time at the moment, and I've been asked to repost the issue here in this forum as it is more relevant here. I have an API endpoint, which is working. However when I attempt to post a file to the endpoint via <input type="file" name="blah"> with enctype="multipart/form-data", IRequest.Files isn't being populated. Lets say the request data from my browser is :- POST /emby/ScripterX/Packages/UploadPackage HTTP/1.1Host: 192.168.1.50:5999Connection: keep-aliveContent-Length: 455Cache-Control: max-age=0Upgrade-Insecure-Requests: 1Origin: nullContent-Type: multipart/form-data; boundary=----WebKitFormBoundaryCWKRTMWE0OAxwgGfUser-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9Accept-Encoding: gzip, deflateAccept-Language: en-US,en;q=0.9------WebKitFormBoundaryCWKRTMWE0OAxwgGfContent-Disposition: form-data; name="PkgFile"; filename="testpackage.zip"Content-Type: application/x-zip-compressedPK¸·P¿r(Utest123.txtthis is a test, 1 2 3.PK¸·P¿r(U test123.txtPK9?------WebKitFormBoundaryCWKRTMWE0OAxwgGfContent-Disposition: form-data; name="btnSubmit"Upload!------WebKitFormBoundaryCWKRTMWE0OAxwgGf-- when handling, you can see File Count = 0. This is from Request.Files.Length 2020-05-24 17:01:33.477 Info HttpServer: HTTP Response 204 to 192.168.1.118. Time: 2ms. http://192.168.1.50:8096/emby/ScripterX/Packages/UploadPackage 2020-05-24 17:01:33.692 Info HttpServer: HTTP POST http://192.168.1.50:8096/emby/ScripterX/Packages/UploadPackage. UserAgent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.362020-05-24 17:01:33.694 Info Emby ScripterX: Api File In > File Count = 0 and the summary data for IRequest.Files is: // Summary: // Access to the multi-part/formdata files posted on this request IHttpFile[] Files { get; } I utilise IRequiresRequest. Request data (other then .Files) is correct - content type, content length, etc exists and works fine. thank you in advance, Anthony 1 Link to comment Share on other sites More sharing options...
Anthony Musgrove 195 Posted May 24, 2020 Author Share Posted May 24, 2020 my initial post is at: https://emby.media/community/index.php?/topic/86570-api-endpoint-file-upload/ 1 Link to comment Share on other sites More sharing options...
Anthony Musgrove 195 Posted May 25, 2020 Author Share Posted May 25, 2020 Getting closer to achieving this another way, but surely IRequest is broken (and I'm not going crazy). Another way I'm going about it until its confirmed, which probably won't be reliable. FileReader() to read the zip file, posting it to the api endpoint as a hidden input, and server-side saving the string content of the zip file to a .zip file.. just comparing. the top one is the string sent, and the bottom one is the actual content of the zip file... so encoding needs to be looked at. But I'd rather do it the right way Link to comment Share on other sites More sharing options...
Anthony Musgrove 195 Posted May 25, 2020 Author Share Posted May 25, 2020 Getting closer now.. data:application/x-zip-compressed;base64,UEsDBBQAAAAAAAO4t1C/cihVFgAAABYAAAALAAAAdGVzdDEyMy50eHR0aGlzIGlzIGEgdGVzdCwgMSAyIDMuUEsBAhQAFAAAAAAAA7i3UL9yKFUWAAAAFgAAAAsAAAAAAAAAAQAgAAAAAAAAAHRlc3QxMjMudHh0UEsFBgAAAAABAAEAOQAAAD8AAAAAAA== Link to comment Share on other sites More sharing options...
Luke 37067 Posted May 25, 2020 Share Posted May 25, 2020 What you should do is have your request object implement IRequiresRequestStream, then access RequestStream directly. When you upload an image using the image editor, this is what happens. 1 Link to comment Share on other sites More sharing options...
Anthony Musgrove 195 Posted May 25, 2020 Author Share Posted May 25, 2020 What you should do is have your request object implement IRequiresRequestStream, then access RequestStream directly. When you upload an image using the image editor, this is what happens. Thank you mate, I have been trying to use the RequestStream, I'll look into it a bit more Does the image editor use <form> file inputs to submit/upload to the endpoint, or is it some type of javascript/ajax submission? Thanks so much Link to comment Share on other sites More sharing options...
Anthony Musgrove 195 Posted May 25, 2020 Author Share Posted May 25, 2020 so I found where the image editor uploads an image. return connectionManager.getApiClient(currentServerId).uploadItemImage(currentItemId, imageType, file).then(function () { Awesome, and it works exactly how I've just implemented it; return new Promise(function (resolve, reject) { var reader = new FileReader; reader.onerror = function () { reject() }, reader.onabort = function () { reject() }, reader.onload = function (e) { var data = e.target.result.split(",")[1]; instance.ajax({ type: "POST", url: url, data: data, contentType: "image/" + file.name.substring(file.name.lastIndexOf(".") + 1) }).then(resolve, reject) }, reader.readAsDataURL(file) That is fantastic to know )))))) Link to comment Share on other sites More sharing options...
Anthony Musgrove 195 Posted May 25, 2020 Author Share Posted May 25, 2020 Thank you so very much guys Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now