File transfer from Browser to locally connected iPhone

2.9k views Asked by At

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.

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.

2

There are 2 answers

1
Isaac Han On

ws: protocol is allowed? For example config.xml

<access origin="ws://192.168.1.xx/*"/>
1
Martin On

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