当前位置:首页 » 文件管理 » 文件上传跨域

文件上传跨域

发布时间: 2022-12-25 07:26:37

java怎样实现跨服务器文件上传

另一台机器也要有处理文件上传的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中添加配置

热点内容
自己怎么搭建网站服务器 发布:2025-08-22 00:36:54 浏览:141
按键精灵只能做手游脚本吗 发布:2025-08-22 00:31:22 浏览:152
php网站制作 发布:2025-08-22 00:31:19 浏览:488
java的http编程 发布:2025-08-21 23:56:32 浏览:988
大学数据库试题 发布:2025-08-21 23:56:28 浏览:801
沾福卡的算法 发布:2025-08-21 23:38:26 浏览:337
java极光 发布:2025-08-21 23:38:14 浏览:709
php路由框架 发布:2025-08-21 23:32:17 浏览:771
超微ipmi无法解析服务器dns地址 发布:2025-08-21 23:31:14 浏览:162
私服魔域脚本 发布:2025-08-21 23:29:34 浏览:55