Right now, I have created an HTTP Server on My iPhone Application and have hosted HTML there. Then Accessing it on Browser of the system that is in the same network of iPhone. I can see the File on my Browser.
Now using WebSockets I am trying to send File from Browser to Application, but It's not working. It's fine with Text Message but Not in case of Data. As a workaround, I tried it via Base64 String, but in that case also socket Get Closed.
For uploading using JAVAScript I have written this code, here I tried by sending Base64 string in fragments of size 200 characters.
function sendFile() {
var preview = document.querySelector('img');
var file = document.querySelector('input[type=file]').files[0];
var reader = new FileReader();
var rawData = new ArrayBuffer();
reader.onloadend = function () {
var stringContent = reader.result;
preview.src = stringContent;
var array = stringContent.match(/.{1,200}/g);
for (var i = 0; i < array.length; i++) {
ws.send(array[i]);
};
}
if (file) {
reader.readAsDataURL(file);
}else {
preview.src = "";
}
}
On iPhone side, I have used WebSocket Class from Libary CocoaHTTPServer
Socket closed at this line.
EDIT
After lots of trial and Error, I come to know that This is happening If I am opening this in Browser of Mac, Not in case of any other devices' browser like iPad, iPhone. This is very weird use-case but its true.
EDIT II
After lots of wondering, I found a Clue to this, This was working nicely for iPhone, iPad, iPod & Opera browsers, because they have old websocket support, i found this from here..
In this question the Guy have the reverse case, He is trying to close the connection on these browsers, in My case It's closing on other Browsers like chrome, Mozilla, etc. It's because something called Hybi formatted packets. This might help someone to suggest the solution for my case.
To download a file in Safari, just tap on a download link on a website or tap and hold on a link and then tap Download Linked File (Figure B). Downloading a file in Safari on iOS or iPadOS is as easy as tapping and holding on a link and selecting Download Linked File.
1. Start the Safari app and go to a web page that has the file you want to download. 2. Tap the file to download it, whether through a link or download button.
I think you should look at the official CocoaHTTPServer examples. There is one for http file uploads: https://github.com/robbiehanson/CocoaHTTPServer/tree/master/Samples/SimpleFileUploadServer
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With