从客户端向服务器发送大文件,我的逻辑正确吗? [英] Sending a large file from a client to a server, is my logic correct?

查看:140
本文介绍了从客户端向服务器发送大文件,我的逻辑正确吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些相对较大的文件(1Mb-32Mb),我需要将它们从桌面应用程序发送到用PHP编写的脚本中(借助于称为CodeIgniter的PHP框架).由于文件很大,因此我正在考虑将这些文件分成小块/数据包,使用Base64对其进行编码,然后通过JSON格式的消息将其一一发送.

I have some relatively large files (1Mb-32Mb) and I need to send them from a desktop app to a script written in PHP (with the aid of a PHP framework called CodeIgniter). Since the files are pretty big I'm considering to split those files into small chunks / packets, encode each of them with Base64 and send them one by one through a JSON-formatted message.

我对如何将它们发送到服务器有一个想法,但是我想先问问您,如果您认为这没问题或有缺陷,您对此有何看法.

I've an idea about how to send them to the server, but I'd like to ask you first what do you think about that, if you think that it's allright or if it's flawed.

是的.由于英语不是我的母语,因此我更喜欢将其公开为客户端和服务器这两个主题之间的对话.我虽然您会更好地理解我要做什么:

Here it is. Since English is not my native language I prefer to expose it as a dialoge between two subjects, a client and a server. I though you would understand a little bit better what I'm trying to do:

消息1:

客户端:服务器,我需要向您发送-X- KB长文件.我将向您发送的文件的校验和为-file_cheksum-".

Client: "Hi server, I need to send you a -X- kbytes long file. The file I'll send you will have a checksum of -file_cheksum-"

服务器:好,我准备好接收它.将其分成-N-个小数据包,每个小数据包的长度为-Y- KB,然后将它们一一发送给我,如果您在邮件中提到将包裹发送到 组装一个ID为-FILE_ID-"

Server: "Ok I'm ready to receive it. Split it in -N- small packets, each with a length of -Y- Kbytes, and send them here one by one, I'll remember what to do with them if in your message you mention that your are sending packages to assemble a file with the ID -FILE_ID- "

消息2:

客户端:这是-N-的第一个数据包,组装一个名为-FILE_ID-的文件时需要此数据包, 这是第一个数据包的校验和,因此您可以检查数据包是否到达良好状态"

Client: "Here's the first packet of -N-, this packet is required to assemble a file named -FILE_ID-, and here's the checksum of the first packet so you can check if the packet has arrived in good shape"

服务器:谢谢客户,我刚刚将其存储在一个临时文件夹中.请继续."

Server: "Thank you client, I've just memorized it in a temporary folder. Keep going."

...

消息#N + 1:

客户端:这是-N-的-N-数据包,这是最后一个数据包,需要汇编一个名为-FILE_ID-的文件, 这是最后一个数据包的校验和,因此您可以检查数据包是否到达良好状态"

Client: "Here's the -N- packet of -N-, this is the last packet and it's required to assemble a file named -FILE_ID-, and here's the checksum of the last packet so you can check if the packet has arrived in good shape"

服务器:谢谢客户,我已经将所有数据包组装在一起并验证了校验和; 生成的文件就可以了.干得好."

Server: "Thank you client, I've just assembled together all the packets and verified the checksum; the resulting file is OK. Good job."

每条消息将通过HTTP Post发送,并使用JSON格式化,每个包含文件一部分的数据包都将在Base64中编码,然后由服务器解码.这是从客户端发送到服务器的消息的示例:

Each message will be sent via HTTP Post and will be formatted with JSON, and each packet containing a part of the file would be encoded in Base64 and then decoded by the server. Here's an example of a message sent from the client to the server:

{
   "request": "set-packet",
   "id": " [ file ID ] ",
   "packet":
   {
      "file": " [ here goes the packet in Base64 ] ",
      "checksum": " [ here goes the checksum ] "
   }
}

如果一切正常,服务器将响应:

If everything is allright, the server would respond:

{
   "status": "ok",
   "message": "packet memorized"
}

如果出现问题,服务器将例如响应:

If there's a problem, the server instead would respond for example:

{
   "status": "error",
   "message": "Checksum doesn't match, try again" 
}

您如何看待这种逻辑?它是正确的还是有缺陷的,您会对此进行任何改进吗?

What do you think about this logic? Is it correct or flawed, and would you make any improvements on it?

谢谢

推荐答案

我认为您建议的解决方案过于矫kill过正.您正在对应用程序层(HTTP)进行传输协议级别检查(数据包校验和).是的,它可以工作,但是却无济于事,使数据包校验和已经在TCP/IP级别(早于HTTP抽象级别)发生了.

I believe your suggested solution is way too much overkill.. You're doing transmission protocol level checks (packet checksums) on an application layer (HTTP). Yes, it could work, but it's litterly doubling the work for nothing, the packet checksums are already happening at a TCP/IP level (long before the HTTP abstraction level).

我也会按照Dagon的建议,使用旨在用于文件传输的协议:FTP(或SSH).任何共享主机都应该具有ftp或ssh(否则,如何首先在服务器上获取任何文件:P).检出ftp http://be.php.net/manual/zh/book. ftp.php 和ssh http://be.php.net/manual/zh-CN/book.ssh2.php php扩展.

I would also go for what Dagon suggested, use a protocol intended for file transfer: FTP (or SSH). Any shared host SHOULD have either ftp or ssh (else, how do you get any of your files on the server in the first place :P). Checkout the ftp http://be.php.net/manual/en/book.ftp.php and ssh http://be.php.net/manual/en/book.ssh2.php extensions of php.

欢呼

这篇关于从客户端向服务器发送大文件,我的逻辑正确吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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