如何将javascript变量作为$ _FILE发送到PHP服务器 [英] How to send javascript variable to PHP server as $_FILE

查看:80
本文介绍了如何将javascript变量作为$ _FILE发送到PHP服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们在服务器上有PHP代码,它接收文件到$ _FILE,然后根据用户/安全考虑因素存储它们。

We have PHP code on a server that receives files into $_FILE and then stores them based on user/security considerations.

在客户端,我们现在可以按原样将文件发送到服务器,或者我们想在本地处理文件(在浏览器的js内存中)和然后将文件传送到服务器。我们可以使用JSON将处理后的文件发布到服务器,但是存在问题。

On the client, we can currently send the file to the server as-is or we would like to process the file locally (in memory in js in the browser) and then deliver the file to the server. We can POST the processed file to the server using JSON, however there are issues with that.

我们更喜欢将客户端的javascript变量的内容发送到服务器在$ _FILE中收到。 (我们不能假设客户端能够在本地保存文件。)我们是否必须以某种方式模仿FORM提交?

We would much prefer to send the contents of the client's javascript variable to the server to be received in $_FILE. (We cannot assume client will have the ability to save the file locally.) Do we have to imitate a FORM submission somehow?

如何发送javascript变量为作为PHP $ _FILE收到?

How to we send a javascript variable to be received as a PHP $_FILE?

谢谢!

更新
Blob肯定是正确的方向,但我们注意到Blob大小比传入它的文件大50%。 $ _FILE中的文件也大50%。
我们已尝试在Blob创建时覆盖文件类型,基于其他帖子,例如 BlobBuilder遗址二进制数据,但它没有固定50%的规模增加。我们根据收到的拖放文件类型设置Blob文件类型。例如,我们上传了一个900K的PDF文件。类型类似于'application / pdf'。得到的斑点就像1,400K。也试过PNG。

UPDATE Blob definitely appears to be the right direction, but we are noticing that the Blob size is coming out 50% larger than the file that goes in to it. The file in $_FILE is also 50% larger. We have tried overriding the file type at Blob creation, based on other posts such as BlobBuilder ruins binary data, however it hasn't fixed the 50% increase in size. We are setting the Blob file type based on the drag and drop file type we receive. For example, we upload a 900K PDF file. Type was something like 'application/pdf'. The resultant blob was like 1,400K. Also tried with PNG.

推荐答案

如果客户支持 Blob 和XHR2( FormData ),那你就是黄金。假设您的数据保存在字符串 data 中,您只需

If the client supports Blob and XHR2 (FormData), then you're gold. Supposing that your data is kept in a string data, all you have to do is

// Set the file media type appropriately
var blob = new Blob([ data ], { type: "image/png" });

var formData = new FormData();
formData.append("theFile", blob, "filename.png");

var xhr = new XMLHttpRequest();
xhr.open("POST", "/destination");
xhr.send(formData);
xhr.onload = function() {
    // ...
};

更新:如果您的客户端不支持XHR2,那么 只是一种使用AJAX发送文件的方法,虽然它是有点复杂

UPDATE: if your client doesn't support XHR2, there is a way to send files using AJAX only, although it's a bit complicated.

基本上,你必须模仿multipart / form-data请求,并手动构建body的各个部分请求。不是很容易,但可行,并且应该适用于IE6-9。

Basically, you have to mimic a multipart/form-data request, and manually build the parts of the body of the request. Not very easy, but feasible, and should work with IE6-9.

这篇关于如何将javascript变量作为$ _FILE发送到PHP服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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