文件上傳跨域
另一台機器也要有處理文件上傳的WEB程序,你可以參考Stream上傳插件(支持HTML5和Flash兩種方式上傳)
Stream 上傳插件
Stream 是解決不同瀏覽器上傳文件的插件,是Uploadify的Flash版和Html5版的結合!
Stream 簡介
Stream 是根據某網的文件上傳插件加工而來,支持不同平台(Windows, Linux, Mac, Android, iOS)下,主流瀏覽器(IE7+, Chrome, Firefox, Safari, 其他)的上傳工作,當然在Html5標准下,還支持文件的斷點續傳功能,有效解決大文件的Web上傳問題!
主要特徵
1. 源碼完全開放,目前有Java、php、Perl三種後台語言實現
2. 支持HTML5、Flash兩種方式(跨域)上傳
3. 多文件一起上傳
4. HTML5支持斷點續傳,拖拽等新特性
5. 兼容性好IE7+, FF3.6+, Chrome*,Safari4+,遨遊等主流瀏覽器
6. 進度條、速度、剩餘時間等附屬信息
7. `選擇文件的按鈕`可以自定義
8. 簡單的參數配置實現 靈活多變的功能
9. 支持文件夾上傳(Chrome21+, Opera15+)
10. 支持自定義UI(V1.4+)
指定跨域上傳就可以了
❷ ajax如何實現跨域上傳文件
ajax跨域實現方法之跨子域實現ajax:
要求:實現hello.com的頁面 非同步請求 blog.hello.com下的頁面
實現方法:藉助iframe,通過設置iframe的src屬性,嵌入blog.hello.com下的一個頁面,比如AjaxProxy.jsp,然後由該頁面去請求Ajax
AjaxProxy請求完畢後,通過parent對象把返回的數據回傳給hello.com的主頁面。因此,真正的非同步請求還是發生在blog.hello.com的域名下
注意:通過這種方法實現的跨子域ajax請求,需要在hello.com的請求頁面以及AjaxProxy.jsp頁面中設置同樣的域名,也就是document.domain = "coolkissbh.com";
注意:關於設置domain的問題,如果是跨全域,使用上面方法時候,firefox下會提示
Illegal document.domain value" code: "1009的錯誤,因此跨全域只能使用第二種方法處理返回的數據:AjaxProxy.jsp將ajax返回的數據保存到一個全局變數中,hello.com通過setInterval定時去判斷iframe的頁面是否載入完成,如果載入完成,則獲取AjaxProxy.jsp的全局變數值。然後再做其它處理。
❸ PHP跨域上傳的幾種方法
方法一:
文件夾:/home/web/attachments
虛擬二級目錄到/home/web/zxsv/下(支持同區域網的伺服器)
這樣多個子域名進行上傳的設計時,只需要attachments目錄映射為相關的域名的二級目錄,這樣就可實現多個子域名共享一個附件伺服器了,這種方法最好是用區域網中的附件伺服器,這樣流量是分開的,當然訪問附件的域名是apache,ngixn,IIS等的虛擬二級目錄就不說了,好處是現有程序不做任何修改,唯一壞處就是兩台伺服器必須在一個區域網中,當然你用單台也就沒這個問題了
方法二:ftp同步更新
PHP是支持FTP的,給個FTP類裡面(不是我寫的,只是加了個建立多級目錄),自己看著辦吧,上傳後調用FTP類,同步到FTP伺服器中,好處是現有程序只需要在上傳那段加上FTP上傳就行了,壞處就是一定要支持FTP
<?php
$ftp=new Ftp;
//print_r($ftp->nlist(」"));
$ftp->makedir(」3″);
//$ftp->put(」comment.php」,」1.txt」);
$ftp->bye();
//R FTP 處理;
class ftp {
var $ftpUrl = 『www.zxsv.com』;
var $ftpUser = 『zxsv』;
var $ftpPass = 『111111′;
var $ftpDir = 『/zxsv/』;
var $ftpR = 」; //R ftp資源;
var $status = 」;
//R 1:成功;2:無法連接ftp;3:用戶錯誤;
function ftp() {
if ($this->ftpR = ftp_connect($this->ftpUrl, 21)) {
if (ftp_login($this->ftpR, $this->ftpUser, $this->ftpPass)) {
if (!empty($this->ftpDir)) {
ftp_chdir($this->ftpR, $this->ftpDir);
}
ftp_pasv($this->ftpR, true);//R 啟用被動模式;
$status = 1;
} else {
$status = 3;
}
} else {
$status = 2;
}
}
//R 切換目錄;
function cd($dir) {
return ftp_chdir($this->ftpR, $dir);
}
//建立目錄
function mkdir($dir){
return ftp_mkdir($this->ftpR, $dir);
}
function makedir($dir) {
if(!$dir) return 0;
$dir = str_replace( 「\\」, 「/」, $dir );
$mdir = 「」;
foreach(explode( 「/」, $dir ) as $val ) {
$mdir .= $val.」/」;
if( $val == 「..」 || $val == 「.」 ) continue;
if(!@mkdir($mdir)){
echo 「創建目錄 [".$mdir."]失敗.」;
//exit;
}
}
return true;
}
//刪除目錄
function rmdir($dir){
return ftp_rmdir($this->ftpR, $dir);
}
//R 返回當前路勁;
function pwd() {
return ftp_pwd($this->ftpR);
}
//R 上傳文件;
function put($localFile, $remoteFile = 」) {
if ($remoteFile == 」) {
$remoteFile = end(explode(』/', $localFile));
}
$res = ftp_nb_put($this->ftpR, $remoteFile, $localFile, FTP_BINARY);
print_r($res);
while ($res == FTP_MOREDATA) {
$res = ftp_nb_continue($this->ftpR);
}
if ($res == FTP_FINISHED) {
return true;
} elseif ($res == FTP_FAILED) {
return false;
}
}
//R 下載文件;
function get($remoteFile, $localFile = 」) {
if ($localFile == 」) {
$localFile = end(explode(』/', $remoteFile));
}
if (ftp_get($this->ftpR, $localFile, $remoteFile, FTP_BINARY)) {
$flag = true;
} else {
$flag = false;
}
return $flag;
}
//R 文件大小;
function size($file) {
return ftp_size($this->ftpR, $file);
}
//R 文件是否存在;
function isFile($file) {
if ($this->size($file) >= 0) {
return true;
} else {
return false;
}
}
//R 文件時間
function fileTime($file) {
return ftp_mdtm($this->ftpR, $file);
}
//R 刪除文件;
function unlink($file) {
return ftp_delete($this->ftpR, $file);
}
function nlist($dir = 『/service/resource/』) {
return ftp_nlist($this->ftpR, $dir);
}
//R 關閉連接;
function bye() {
return ftp_close($this->ftpR);
}
}
?>
❹ 用webuploader怎麼解決跨域上傳文件的問題
最近研究了下大文件上傳的方法,找到了webuploader js 插件進行大文件上傳,大家也可以參考這篇文章進行學習:《Web Uploader文件上傳插件使用詳解》 使用 使用webuploader分成簡單直選要引入 <!--引入CSS--> <link rel="stylesheet" type="text/css" href="webuploader文件夾/webuploader.css"> <!--引入JS--> <script type="text/javascript" src="webuploader文件夾/webuploader.js"></script> HTML部分 <div id="uploader" class="wu-example"> <!--用來存放文件信息--> <div id="thelist" class="uploader-list"></div> <div class="btns"> <div id="picker">選擇文件</div> <button id="ctlBtn" class="btn btn-default">開始上傳 </button> </div> </div> 初始化Web Uploader jQuery(function() { $list = $('#thelist'), $btn = $('#ctlBtn'), state = 'pending', uploader; uploader = WebUploader.create({ // 不壓縮image resize: false, // swf文件路徑 swf: 'uploader.swf', // 文件接收服務端。 server: upload.php, // 選擇文件的按鈕。可選。 // 內部根據當前運行是創建,可能是input元素,也可能是flash. pick: '#picker', chunked: true, chunkSize:2*1024*1024, auto: true, accept: { title: 'Images', extensions: 'gif,jpg,jpeg,bmp,png', mimeTypes: 'image/*' } }); upload.php處理 以下是根據別人的例子自己拿來改的php 後台代碼 header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); header("Cache-Control: no-store, no-cache, must-revalidate"); header("Cache-Control: post-check=0, pre-check=0", false); header("Pragma: no-cache"); if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') { exit; // finish preflight CORS requests here } if ( !empty($_REQUEST[ 'debug' ]) ) { $random = rand(0, intval($_REQUEST[ 'debug' ]) ); if ( $random === 0 ) { header("HTTP/1.0 500 Internal Server Error"); exit; } } // header("HTTP/1.0 500 Internal Server Error"); // exit; // 5 minutes execution time @set_time_limit(5 * 60); // Uncomment this one to fake upload time // usleep(5000); // Settings // $targetDir = ini_get("upload_tmp_dir") . DIRECTORY_SEPARATOR . "plupload"; $targetDir = 'uploads'.DIRECTORY_SEPARATOR.'file_material_tmp'; $uploadDir = 'uploads'.DIRECTORY_SEPARATOR.'file_material'; $cleanupTargetDir = true; // Remove old files $maxFileAge = 5 * 3600; // Temp file age in seconds // Create target dir if (!file_exists($targetDir)) { @mkdir($targetDir); } // Create target dir if (!file_exists($uploadDir)) { @mkdir($uploadDir); } // Get a file name if (isset($_REQUEST["name"])) { $fileName = $_REQUEST["name"]; } elseif (!empty($_FILES)) { $fileName = $_FILES["file"]["name"]; } else { $fileName = uniqid("file_"); } $oldName = $fileName; $filePath = $targetDir . DIRECTORY_SEPARATOR . $fileName; // $uploadPath = $uploadDir . DIRECTORY_SEPARATOR . $fileName; // Chunking might be enabled $chunk = isset($_REQUEST["chunk"]) ? intval($_REQUEST["chunk"]) : 0; $chunks = isset($_REQUEST["chunks"]) ? intval($_REQUEST["chunks"]) : 1; // Remove old temp files if ($cleanupTargetDir) { if (!is_dir($targetDir) !$dir = opendir($targetDir)) { die('{"jsonrpc" : "2.0", "error" : {"code": 100, "message": "Failed to open temp directory."}, "id" : "id"}'); } while (($file = readdir($dir)) !== false) { $tmpfilePath = $targetDir . DIRECTORY_SEPARATOR . $file; // If temp file is current file proceed to the next if ($tmpfilePath == "{$filePath}_{$chunk}.part" $tmpfilePath == "{$filePath}_{$chunk}.parttmp") { continue; } // Remove temp file if it is older than the max age and is not the current file if (preg_match('/\.(partparttmp)$/', $file) && (@filemtime($tmpfilePath) < time() - $maxFileAge)) { @unlink($tmpfilePath); } } closedir($dir); } // Open temp file if (!$out = @fopen("{$filePath}_{$chunk}.parttmp", "wb")) { die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}'); } if (!empty($_FILES)) { if ($_FILES["file"]["error"] !is_uploaded_file($_FILES["file"]["tmp_name"])) { die('{"jsonrpc" : "2.0", "error" : {"code": 103, "message": "Failed to move uploaded file."}, "id" : "id"}'); } // Read binary input stream and append it to temp file if (!$in = @fopen($_FILES["file"]["tmp_name"], "rb")) { die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}'); } } else { if (!$in = @fopen("php://input", "rb")) { die('{"jsonrpc" : "2.0", "error" : {"code": 101, "message": "Failed to open input stream."}, "id" : "id"}'); } } while ($buff = fread($in, 4096)) { fwrite($out, $buff); } @fclose($out); @fclose($in); rename("{$filePath}_{$chunk}.parttmp", "{$filePath}_{$chunk}.part"); $index = 0; $done = true; for( $index = 0; $index < $chunks; $index++ ) { if ( !file_exists("{$filePath}_{$index}.part") ) { $done = false; break; } } if ( $done ) { $pathInfo = pathinfo($fileName); $hashStr = substr(md5($pathInfo['basename']),8,16); $hashName = time() . $hashStr . '.' .$pathInfo['extension']; $uploadPath = $uploadDir . DIRECTORY_SEPARATOR .$hashName; if (!$out = @fopen($uploadPath, "wb")) { die('{"jsonrpc" : "2.0", "error" : {"code": 102, "message": "Failed to open output stream."}, "id" : "id"}'); } if ( flock($out, LOCK_EX) ) { for( $index = 0; $index < $chunks; $index++ ) { if (!$in = @fopen("{$filePath}_{$index}.part", "rb")) { break; } while ($buff = fread($in, 4096)) { fwrite($out, $buff); } @fclose($in); @unlink("{$filePath}_{$index}.part"); } flock($out, LOCK_UN); } @fclose($out); $response = [ 'success'=>true, 'oldName'=>$oldName, 'filePaht'=>$uploadPath, 'fileSize'=>$data['size'], 'fileSuffixes'=>$pathInfo['extension'], 'file_id'=>$data['id'], ]; die(json_encode($response)); } // Return Success JSON-RPC response die('{"jsonrpc" : "2.0", "result" : null, "id" : "id"}'); 以上就是本文的全部內容,希望對大家的學習有所幫助。
❺ 用webuploader怎麼解決跨域上傳文件的問題
跨域請求前瀏覽器會自動發出一個options請求,如果伺服器的響應頭部中有如下信息:
Access-Control-Allow-Origin: #允許訪問的源,如ht localhost:3000
Access-Control-Allow-Methods: #允許的方法,如get, post
瀏覽器收到這個響應就會繼續原來的請求,否則就會終止。
在webuploader中可以在uploadBeforeSend的回調中設置請求的頭部,例如
uploader.on('uploadBeforeSend', function(obj, data, headers) {
_.extend(headers, {
"Origin": "h /localhost:3000",
"Access-Control-Request-Method": "POST"
❻ 上傳文件時出現跨域問題
一個新的奇葩問題:前端報跨域出錯,原因卻在後台上傳的文件超過了Tomcat限制。
前端報錯
後端日誌
所以啊,這根本不是跨域的問題, Tomcat默認上傳的文件大小就是1MB ,你上傳的文件超過而已。
你可以在前端配置一下文件大小限制,
例如
或者在後端設置上傳文件大小限制
以SpringBoot為例
在application.yml中添加配置