如何使用jquery blueimp将文件上传到远程服务器

时间:2013-10-05 21:43:56

标签: php jquery blueimp

我正在使用https://github.com/blueimp/jQuery-File-Upload/插件。

如何设置此插件,以便我可以将文件上传到随机远程服务器。

我有5台服务器,其中包含以下网址

  1. mydomain.com/files
  2. mydomain2.com/files
  3. mydomain3.com/files
  4. mydomain4.com/files
  5. mydomain5.com/files
  6. *可以在需要时动态添加更多服务器。

    我是否需要将uploadhandler类(php)上传到每台服务器?或者我还需要做什么?

2 个答案:

答案 0 :(得分:0)

你必须做几件事,

首先是随机服务器

server_pool = ["mydomain.com/files","mydomain2.com/files","mydomain3.com/files","mydomain4.com/files","mydomain5.com/files"];
var theserver = server_pool[Math.floor(Math.random() * server_pool.length)];

现在我们要将随机服务器添加到blueimp中的de main.js文件中。

替换此

$('#fileupload').fileupload({
    // Uncomment the following to send cross-domain cookies:
    //xhrFields: {withCredentials: true},
    url: 'server/php/'
});

为此:

$('#fileupload').fileupload({
    // Uncomment the following to send cross-domain cookies:
    //xhrFields: {withCredentials: true},
    // url: 'server/php/' OLD ONE
    url: theserver
});

现在是PHP部件的时候了,您需要将server / php文件夹上传到所有其他服务器,但首先需要将其添加到其中的index.php文件中。

在此之后添加此权限

这是结果:

<?php
header('Access-Control-Allow-Origin: *');
/*
 * jQuery File Upload Plugin PHP Example 5.14
 * https://github.com/blueimp/jQuery-File-Upload
 *
 * Copyright 2010, Sebastian Tschan
 * https://blueimp.net
 *
 * Licensed under the MIT license:
 * http://www.opensource.org/licenses/MIT
 */

error_reporting(E_ALL | E_STRICT);
require('UploadHandler.php');
$upload_handler = new UploadHandler();

干杯

答案 1 :(得分:0)

首先将文件上传到主服务器,然后使用curl

将其发送到所需的服务器
$curl_connection = curl_init('http://serverX.exemple.com/upload');
curl_setopt($curl_connection, CURLOPT_POST, TRUE);  

curl_setopt($curl_connection, CURLOPT_POSTFIELDS, array(
    'upload' => '@' . "path/to/file1.ext",

    ));

curl_setopt($curl_connection, CURLOPT_RETURNTRANSFER, 1);
echo curl_exec($curl_connection) ;
curl_close($curl_connection);