本文目錄一覽:
Java中如何圖片非同步上傳
在java中要實現非同步上傳要提前做好準備,對於文件上傳,瀏覽器在上傳的過程中是將文件以流的形式提交到伺服器端的,如果直接使用Servlet獲取上傳文件的輸入流然後再解析裡面的請求參數是比較麻煩,所以一般選擇採用apache的開源工具common-fileupload這個文件上傳組件。
這個common-fileupload上傳組件的jar包可以去apache官網上面下載,也可以在struts的lib文件夾下面找到,struts上傳的功能就是基於這個實現的。
common-fileupload是依賴於common-io這個包的,所以還需要下載這個包。剩下的就是js文件的導入了,我導入了以下文件:
script type=”text/javascript” src=”lib/Js/jquery.js”/script
script ltype=”text/javascript” src=”/js/ajaxfileupload.js”/script
在頁面中的寫法:
div class=”controls”span class=”btn green fileinput-button”i class=”icon-plus icon-white”/i
span上傳照片/span
input id=”fileToUpload” name=”myfiles” type=”file” onchange=”upload()” title=”上傳” //span
/divfunction upload(){
$.ajaxFileUpload
(
{
url:’%=basePath%sysperson/uploadpic’,
secureuri:false,
fileElementId:’fileToUpload’,
dataType: ‘text’,
success: function (data, status)
{
document.all.mypic.src=”%=basePath%uploads/” + data;
document.all.picpath.value = data;
}, error : function(data, status, e) {
alert(e);
}
});
}
Java中如何實現Ajax方式上傳圖片?
你好,據我所知,Ajax不能上傳圖片,因為上傳圖片得把圖片的二進位數據傳送給WEB伺服器。而Ajax不能完成。
如果樓主指點是無刷新上傳圖片。即不更改當前頁面上傳圖片,利用STRUTS並在HTML頁面中加一個iframe標籤,CSS DISPLAY屬性為NONE 而上傳圖片的表單的TARGET屬性等於這個iframe的名字。
springmvc+ajax上傳圖片的問題。傳過去的是空值.怎麼接收圖片??
因為SpringMVC只有GET請求才能通過方法上加參數獲取到值,POST是不能通過這種方式獲取的,可以通過request.getParameter(key) 或者 封裝成對象(屬性對應前端參數)會自動填充。
另外我記得Ajax上傳文件不能直接用$.ajax這種方式傳,我的方法如下:
var form = new FormData();
var xhr = new XMLHttpRequest();
xhr.open(“post”, “url”, true);
xhr.onload = function () {
alert(“上傳完成!”);
};
xhr.send(form);
ajax如何 實現 文件上傳
程序說明
使用說明
實例化時,第一個必要參數是file控制項對象:
new QuickUpload(file);
第二個可選參數用來設置系統的默認屬性,包括
屬性: 默認值//說明
parameter: {},//參數對象
action: “”,//設置action
timeout: 0,//設置超時(秒為單位)
onReady: function(){},//上傳準備時執行
onFinish: function(){},//上傳完成時執行
onStop: function(){},//上傳停止時執行
onTimeout: function(){}//上傳超時時執行
還提供了以下方法:
upload:執行上傳操作;
stop:停止上傳操作;
dispose:銷毀程序。
var QuickUpload = function(file, options) {
this.file = $$(file);
this._sending = false;//是否正在上傳
this._timer = null;//定時器
this._iframe = null;//iframe對象
this._form = null;//form對象
this._inputs = {};//input對象
this._fFINISH = null;//完成執行函數
$$.extend(this, this._setOptions(options));
};
QuickUpload._counter = 1;
QuickUpload.prototype = {
//設置默認屬性
_setOptions: function(options) {
this.options = {//默認值
action: “”,//設置action
timeout: 0,//設置超時(秒為單位)
parameter: {},//參數對象
onReady: function(){},//上傳準備時執行
onFinish: function(){},//上傳完成時執行
onStop: function(){},//上傳停止時執行
onTimeout: function(){}//上傳超時時執行
};
return $$.extend(this.options, options || {});
},
//上傳文件
upload: function() {
//停止上一次上傳
this.stop();
//沒有文件返回
if ( !this.file || !this.file.value ) return;
//可能在onReady中修改相關屬性所以放前面
this.onReady();
//設置iframe,form和表單控制項
this._setIframe();
this._setForm();
this._setInput();
//設置超時
if ( this.timeout 0 ) {
this._timer = setTimeout( $$F.bind(this._timeout, this), this.timeout * 1000 );
}
//開始上傳
this._form.submit();
this._sending = true;
},
//設置iframe
_setIframe: function() {
if ( !this._iframe ) {
//創建iframe
var iframename = “QUICKUPLOAD_” + QuickUpload._counter++,
iframe = document.createElement( $$B.ie ? “iframe name=\”” + iframename + “\”” : “iframe”);
iframe.name = iframename;
iframe.style.display = “none”;
//記錄完成程序方便移除
var finish = this._fFINISH = $$F.bind(this._finish, this);
//iframe載入完後執行完成程序
if ( $$B.ie ) {
iframe.attachEvent( “onload”, finish );
} else {
iframe.onload = $$B.opera ? function(){ this.onload = finish; } : finish;
}
//插入body
var body = document.body; body.insertBefore( iframe, body.childNodes[0] );
this._iframe = iframe;
}
},
//設置form
_setForm: function() {
if ( !this._form ) {
var form = document.createElement(‘form’), file = this.file;
//設置屬性
$$.extend(form, {
target: this._iframe.name, method: “post”, encoding: “multipart/form-data”
});
//設置樣式
$$D.setStyle(form, {
padding: 0, margin: 0, border: 0,
backgroundColor: “transparent”, display: “inline”
});
//提交前去掉form
file.form $$E.addEvent(file.form, “submit”, $$F.bind(this.dispose, this));
//插入form
file.parentNode.insertBefore(form, file).appendChild(file);
this._form = form;
}
//action可能會修改
this._form.action = this.action;
},
//設置input
_setInput: function() {
var form = this._form, oldInputs = this._inputs, newInputs = {}, name;
//設置input
for ( name in this.parameter ) {
var input = form[name];
if ( !input ) {
//如果沒有對應input新建一個
input = document.createElement(“input”);
input.name = name; input.type = “hidden”;
form.appendChild(input);
}
input.value = this.parameter[name];
//記錄當前input
newInputs[name] = input;
//刪除已有記錄
delete oldInputs[name];
}
//移除無用input
for ( name in oldInputs ) { form.removeChild( oldInputs[name] ); }
//保存當前input
this._inputs = newInputs;
},
//停止上傳
stop: function() {
if ( this._sending ) {
this._sending = false;
clearTimeout(this._timer);
//重置iframe
if ( $$B.opera ) {//opera通過設置src會有問題
this._removeIframe();
} else {
this._iframe.src = “”;
}
this.onStop();
}
},
//銷毀程序
dispose: function() {
this._sending = false;
clearTimeout(this._timer);
//清除iframe
if ( $$B.firefox ) {
setTimeout($$F.bind(this._removeIframe, this), 0);
} else {
this._removeIframe();
}
//清除form
this._removeForm();
//清除dom關聯
this._inputs = this._fFINISH = this.file = null;
},
//清除iframe
_removeIframe: function() {
if ( this._iframe ) {
var iframe = this._iframe;
$$B.ie ? iframe.detachEvent( “onload”, this._fFINISH ) : ( iframe.onload = null );
document.body.removeChild(iframe); this._iframe = null;
}
},
//清除form
_removeForm: function() {
if ( this._form ) {
var form = this._form, parent = form.parentNode;
if ( parent ) {
parent.insertBefore(this.file, form); parent.removeChild(form);
}
this._form = this._inputs = null;
}
},
//超時函數
_timeout: function() {
if ( this._sending ) { this._sending = false; this.stop(); this.onTimeout(); }
},
//完成函數
_finish: function() {
if ( this._sending ) { this._sending = false; this.onFinish(this._iframe); }
}
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/158370.html