本文介绍了文件从浏览器传输到本地连接的iPhone的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在,我已经在我的iPhone应用程序上创建了一个HTTP服务器,并在那里托管了HTML。然后在浏览器上访问位于iPhone同一网络中的系统。我可以在浏览器上看到文件。

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.

现在使用WebSockets我正在尝试将文件从浏览器发送到应用程序, 但它无法正常工作。文本消息很好但不是数据的情况。
作为一种解决方法,我通过Base64字符串尝试了它,但在这种情况下插座也是封闭的。

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.

用于上传JAVAScript我编写了这段代码,在这里我尝试通过发送大小为200个字符的片段的Base64字符串。

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 = "";
     }
}

在iPhone方面,我使用了 WebSocket 来自Libary的类 CocoaHTTPServer

On iPhone side, I have used WebSocket Class from Libary CocoaHTTPServer

此行已关闭套接字。

编辑

经过大量试用和错误后,我才知道发生了这种情况如果我在浏览器中打开这个Mac,不支持任何其他设备的浏览器,如iPad,iPhone。这是非常奇怪的用例,但确实如此。

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

经过大量的琢磨,我找到了一个线索,这对于iPhone,iPad,iPod和iPad来说效果很好。 Opera浏览器,因为它们有旧的websocket支持,我从。

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..

在这个问题中,Guy有相反的情况,他试图关闭这些浏览器的连接,在我的情况下,它正在关闭其他浏览器,如chrome,Mozilla等这是因为一些名为 Hybi格式的数据包。这可能有助于某人为我的案例建议解决方案。

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.

推荐答案

我认为你应该看看官方的CocoaHTTPServer示例。有一个用于http文件上传:

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

这篇关于文件从浏览器传输到本地连接的iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 13:46
查看更多