通过HTTP上传大文件 [英] Uploading big files over HTTP

查看:622
本文介绍了通过HTTP上传大文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从桌面应用程序向服务器上传可能的大(从10到100的兆字节)文件。服务器代码是用PHP编写的,桌面应用程序在C ++ / MFC中。我希望能够在上传失败中途恢复文件上传,因为此软件将用于不可靠的连接。我有什么选择?我找到了许多用于C ++的HTTP上传组件,例如 http://www.chilkatsoft.com/refdoc/vcCkUploadRef .html 看起来不错,但它似乎不处理一半完成上传的恢复(我假设这是因为HTTP 1.1不支持它)。我也看了BITS服务,但对于上传它需要一个IIS服务器。到目前为止,我唯一的选择似乎是把我想要上传到更小的文件(说每个1兆),将它们全部上传到服务器,使用PHP重新组装它们,并运行一个校验和,看看是否一切正常。要恢复,我需要在上传开始时进行某种形式的握手,以确定服务器上已经有哪些片段。我必须手动编码或任何人知道一个图书馆为我做这一切,或者甚至一个完全不同的解决方案?我不想切换到支持恢复的协议,因为维护原因(防火墙等的潜在问题)。

解决方案

八个月后,但我只是偶然发现这个问题,并感到惊讶,webDAV没有提到。您可以使用HTTP PUT方法上传,并包含一个Content-Range标头来处理恢复等。 HEAD请求会告诉你文件是否已经存在,以及它是多大。所以可能这样:



1)HEAD远程文件



2)如果存在, =本地大小,上传已经完成



3)如果size <



4)使用PUT请求上传文件(或其中的一部分)文件,如果恢复)



5)如果在PUT请求期间连接失败,请从第1步开始。



也可以列出(PROPFIND)和重命名(MOVE)文件,并使用dav创建目录(MKCOL)。



我相信Apache和Lighttpd都有dav扩展。 >

I need to upload potentially big (as in, 10's to 100's of megabytes) files from a desktop application to a server. The server code is written in PHP, the desktop application in C++/MFC. I want to be able to resume file uploads when the upload fails halfway through because this software will be used over unreliable connections. What are my options? I've found a number of HTTP upload components for C++, such as http://www.chilkatsoft.com/refdoc/vcCkUploadRef.html which looks excellent, but it doesn't seem to handle 'resume' of half done uploads (I assume this is because HTTP 1.1 doesn't support it). I've also looked at the BITS service but for uploads it requires an IIS server. So far my only option seems to be to cut up the file I want to upload into smaller pieces (say 1 meg each), upload them all to the server, reassemble them with PHP and run a checksum to see if everything went ok. To resume, I'd need to have some form of 'handshake' at the beginning of the upload to find out which pieces are already on the server. Will I have to code this by hand or does anyone know of a library that does all this for me, or maybe even a completely different solution? I'd rather not switch to another protocol that supports resume natively for maintenance reasons (potential problems with firewalls etc.)

解决方案

I'm eight months late, but I just stumbled upon this question and was surprised that webDAV wasn't mentioned. You could use the HTTP PUT method to upload, and include a Content-Range header to handle resuming and such. A HEAD request would tell you if the file already exists and how big it is. So perhaps something like this:

1) HEAD the remote file

2) If it exists and size == local size, upload is already done

3) If size < local size, add a Content-Range header to request and seek to the appropriate location in local file.

4) Make PUT request to upload the file (or portion of the file, if resuming)

5) If connection fails during PUT request, start over with step 1

You can also list (PROPFIND) and rename (MOVE) files, and create directories (MKCOL) with dav.

I believe both Apache and Lighttpd have dav extensions.

这篇关于通过HTTP上传大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆