12

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.

Community
  • 1
  • 1
Mrug
  • 4,963
  • 2
  • 31
  • 53
  • Why are you using websockets for a file upload. Wouldn't a normal html form (maybe with a litle ajax) do the job? – Martin Jun 18 '15 at 19:58
  • Can you provide detailing please ? – Mrug Jun 19 '15 at 06:25
  • WebSocket is used for bidirectional comunication, means you can send data from the server to a connected client when ever you want and viceversa. For upload a file to the server this is not neccesary, just use a plugin like JQuery Upload (https://blueimp.github.io/jQuery-File-Upload/), and the things becomes much more easier. – user1977204 Nov 18 '15 at 18:52

2 Answers2

1

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

Martin
  • 665
  • 1
  • 7
  • 24
  • This might be a workaround, but I am actually looking for the solution using WebSockets. – Mrug Jun 22 '15 at 06:16
0

ws: protocol is allowed? For example config.xml

<access origin="ws://192.168.1.xx/*"/>
Isaac Han
  • 348
  • 3
  • 8