[OSX Core Foundation]如何通过 HTTP 异步上传文件并在发送流字节时调用回调?

发布于 2024-10-05 08:30:00 字数 737 浏览 0 评论 0原文

在 MacOSX 上,使用 Core Foundation,我想通过 REST API 将大文件(数百兆字节)上传到远程服务器。 由于文件很大,而且我还需要给用户一些反馈,所以我想实现一个简历上传功能,并向用户反馈写入的字节数。

我首先遵循 Apple Guide 进行 CFNetwork 编程: http://developer.apple.com/library/ios/#documentation/Networking/Conceptual/CFNetwork/CFFTPTasks/CFFTPTasks.html#//apple_ref/doc/uid /TP30001132-CH9-SW1 但文件的异步上传仅适用于FTP。

我尝试使用 CFReadStreamCreateForHTTPRequest 但我只收到响应回调。 我尝试使用 CFReadStreamCreateForHTTPStreamedRequest 并在 ReadStreamRef 主体参数上设置了委托,但即使我在实际在运行循环上调度流之前打开流,它也永远不会被调用。

如果有人有一些关于如何做到这一点的提示,那就太好了。 多谢!

-- 雷米

on MacOSX, with Core Foundation, I want to upload a large file (several hundreds of megabytes) to a remote server through a REST API.
Since the file is big and I also need to give the user some feedback, I want to implement a resume upload feature and gives the user feedback on the number of bytes written.

I first followed the Apple Guide for CFNetwork programming: http://developer.apple.com/library/ios/#documentation/Networking/Conceptual/CFNetwork/CFFTPTasks/CFFTPTasks.html#//apple_ref/doc/uid/TP30001132-CH9-SW1
But the asynchronous upload of file is for FTP only.

I tried to use CFReadStreamCreateForHTTPRequest butI only got callbacks on response.
I tried with CFReadStreamCreateForHTTPStreamedRequest and I set a delegate on the ReadStreamRef body parameter but it is never called even though I open the stream before actually scheduling it on the runloop.

If somebody has some tips about how to do it, it would be great.
Thanks a lot!

--
Rémy

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

妄断弥空 2024-10-12 08:30:00

我在这里得到了答案: http://lists.apple.com/archives /macnetworkprog/2010/Dec/msg00000.html

CFReadStreamCreateForHTTPStreamedRequest is the good function to use.

对于上传反馈,我在创建请求时使用在运行循环上安排的计时器:

CFRunLoopTimerCreate(kCFAllocatorDefault, 0, 10.0, 0, 0, ...);

对于恢复,有两个步骤。

在正确的偏移处寻找本地内容流

创建本地内容流(但尚未打开)后,我可以使用

CFReadStreamSetProperty(content_stream, kCFStreamPropertyFileCurrentOffset, uploaded_length);

配置http标头

在其中进行查找,我没有webdav 风格的远程服务器,因此我使用 HTTP Range 标头来通知服务器我要上传文件的哪一部分。此步骤取决于远程服务器的期望。

CFHTTPMessageSetHeaderFieldValue(request_headers, CFSTR("Range"), content_range_value);

希望这会有所帮助。

I got an answer here: http://lists.apple.com/archives/macnetworkprog/2010/Dec/msg00000.html.

CFReadStreamCreateForHTTPStreamedRequest is the good function to use.

For upload feedback, I use a timer scheduled on the runloop when creating the request:

CFRunLoopTimerCreate(kCFAllocatorDefault, 0, 10.0, 0, 0, ...);

For resume, there are two steps.

Seek the local content stream at the good offset

Once the local content stream created (but not yet opened), I can seek in it using

CFReadStreamSetProperty(content_stream, kCFStreamPropertyFileCurrentOffset, uploaded_length);

Configure http headers

I don't have webdav style remote server, so I use HTTP Range headers to inform the server about which part of the file I want to upload. This step depends on what the remote server expects.

CFHTTPMessageSetHeaderFieldValue(request_headers, CFSTR("Range"), content_range_value);

Hope this will help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文