jQuery上傳文件非同步
① jqueryfileupload 怎麼取消上傳
jquery非同步上傳,一般來說這里上傳調用的是系統專門上傳的action,上傳好後返回上傳文件信息。你這里result.files就是返回的上傳結果。這個需要你在後台自己封裝。你前端需要什麼,後台就封裝什麼。
比如我以前寫過一個
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Map<String, Object> fileObject = new HashMap<String, Object>();
fileinfo.put("size", size);//原始文件大小
fileObject.put("original", original);//原始文件唯一標識
fileObject.put("originalPath", originalPath);//原始文件臨時存儲目錄
fileObject.put("thumb", thumb);//圖片的預覽文件唯一標識
fileObject.put("thumbPath", thumbPath);//圖片預覽文件臨時存儲目錄
fileObject.put("name", fileFileName);//原始圖片名稱
fileObject.put("url", url);//原始圖片的web查看地址,這個可以設置img.src屬性
fileObject.put("thumbnailUrl", thumbnailUrl);//預覽圖片的web查看地址
fileObject.put("contentType", fileContentType);//上傳文件type
fileObject.put("deleteType", "POST");//這是我自己封裝的post刪除
//這個是我自己封裝的刪除路徑
fileObject.put("deleteUrl", super.getRequest().getContextPath() + "/removeUpload.do?id=" + original);
Map[] fileArray = new HashMap[1];
fileArray[0] = fileObject;
JSONObject jsonObject = new JSONObject();
jsonObject.put("files", JSONArray.fromObject(fileArray));
HttpServletResponse response = getResponse();
response.setContentType("application/json;charset=UTF-8");
response.getWriter().write(jsonObject.toString());
response.getWriter().flush();
而前斷可以將上傳文件的唯一標識放到一個隱藏域里,表單提交的時候一起提發送到後台,再根據唯一標識去取上傳文件信息或寫或復制轉移。
② 求一段JS或Jquery非同步上傳圖片的代碼
圖片和文件等流媒體 上傳都是靠from表單的提交。
你可以設置一個隱藏的from表單
裡面有個<input id='file' type='file'>
選擇玩圖片之後賦值給file
然後用jquery from表單提交即可
<formid="form"runat="server"enctype="multipart/form-data">
<inputid='file'type='file'>
</from>
$.ajax({
url:'XXXX',//上傳後台路徑
data:$('#form').serialize(),
type:"POST",
success:function(){
}
});
③ jsp中使用jquery的ajaxfileupload插件怎麼實現非同步上傳
<script type=text/javascript src=js/jquery.js</script <script type=text/javascript src=js/ajaxfileupload.js</script <!-- 執行上傳文件操作的函數 -- <script type=text/javascript function ajaxFileUpload(){ $.ajaxFileUpload({url:'update.do?method=uploader', //需要鏈接到伺服器地址 secureuri:false, fileElementId:'houseMaps', //文件選擇框的id屬性 dataType: 'xml', //伺服器返回的格式,可以是json success: function (data, status) //相當於java中try語句塊的用法{$('#result').html('添加成功');}, error: function (data, status, e) //相當於java中catch語句塊的用法{$('#result').html('添加失敗');}});}</script</head<body<form method=post action=update.do?method=uploader enctype=multipart/form-data <input type=file id=houseMaps name=houseMaps/ <input type=button value=提交 onclick=ajaxFileUpload()/</form<div id=result</div</body</html伺服器代碼: public class UpdateAction extends DispatchAction { public ActionForward uploader(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { UpFormForm upFormForm = (UpFormForm) form; FormFile ff = upFormForm.getHouseMaps();try {InputStream is = ff.getInputStream(); File file = new File(D:/ + ff.getFileName()); //指定文件存儲的路徑和文件名 OutputStream os = new FileOutputStream(file); byte[] b = new byte[1024]; int len = 0; while((len = is.read(b)) != -1){ os.write(b, 0, len);}os.close(); is.close(); } catch (Exception e) {
④ jsp中使用jquery的ajaxfileupload插件怎麼實現非同步上傳
JSP頁面中引入的script代碼
<script>
function ajaxFileUpload()
{
$("#loading").ajaxStart(function(){
$(this).show();
})//開始上傳文件時顯示一個圖片
.ajaxComplete(function(){
$(this).hide();
});//文件上傳完成將圖片隱藏起來
$.ajaxFileUpload({
url:'AjaxImageUploadAction.action',//用於文件上傳的伺服器端請求地址
secureuri:false,//一般設置為false
fileElementId:'imgfile',//文件上傳空間的id屬性 <input type="file" id="imgfile" name="file" />
dataType: 'json',//返回值類型 一般設置為json
success: function (data, status) //伺服器成功響應處理函數
{
alert(data.message);//從伺服器返回的json中取出message中的數據,其中message為在struts2中定義的成員變數
if(typeof(data.error) != 'undefined')
{
if(data.error != '')
{
alert(data.error);
}else
{
alert(data.message);
}
}
},
error: function (data, status, e)//伺服器響應失敗處理函數
{
alert(e);
}
}
)
return false;
}
</script>
struts.xml配置文件中的配置方法:
<struts>
<package name="struts2" extends="json-default">
<action name="AjaxImageUploadAction" class="com.test.action.ImageUploadAction">
<result type="json" name="success">
<param name="contentType">text/html</param>
</result>
<result type="json" name="error">
<param name="contentType">text/html</param>
</result>
</action>
</package>
</struts>
上傳處理的Action ImageUploadAction.action
package com.test.action;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Arrays;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.ActionSupport;
@SuppressWarnings("serial")
public class ImageUploadAction extends ActionSupport {
private File imgfile;
private String imgfileFileName;
private String imgfileFileContentType;
private String message = "你已成功上傳文件";
public File getImgfile() {
return imgfile;
}
public void setImgfile(File imgfile) {
this.imgfile = imgfile;
}
public String getImgfileFileName() {
return imgfileFileName;
}
public void setImgfileFileName(String imgfileFileName) {
this.imgfileFileName = imgfileFileName;
}
public String getImgfileFileContentType() {
return imgfileFileContentType;
}
public void setImgfileFileContentType(String imgfileFileContentType) {
this.imgfileFileContentType = imgfileFileContentType;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
@SuppressWarnings("deprecation")
public String execute() throws Exception {
String path = ServletActionContext.getRequest().getRealPath("/upload/mri_img_upload");
String[] imgTypes = new String[] { "gif", "jpg", "jpeg", "png","bmp" };
try {
File f = this.getImgfile();
String fileExt = this.getImgfileFileName().substring(this.getImgfileFileName().lastIndexOf(".") + 1).toLowerCase();
/*
if(this.getImgfileFileName().endsWith(".exe")){
message="上傳的文件格式不允許!!!";
return ERROR;
}*/
/**
* 檢測上傳文件的擴展名是否合法
* */
if (!Arrays.<String> asList(imgTypes).contains(fileExt)) {
message="只能上傳 gif,jpg,jpeg,png,bmp等格式的文件!";
return ERROR;
}
FileInputStream inputStream = new FileInputStream(f);
FileOutputStream outputStream = new FileOutputStream(path + "/"+ this.getImgfileFileName());
byte[] buf = new byte[1024];
int length = 0;
while ((length = inputStream.read(buf)) != -1) {
outputStream.write(buf, 0, length);
}
inputStream.close();
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
message = "文件上傳失敗了!!!!";
}
return SUCCESS;
}
}
轉載,僅供參考。
⑤ jquery升級後,上傳的文件無效
升級後文件類型不匹配,無法上傳。
在 IE8/9 中使用 JQuery 上傳只能使用 Form 的方式。
使用 JQuery.Ajax 無法上傳文件(因為無法使用 FormData,FormData 是 HTML5 的一個特性,IE8/9 不支持)
使用 JQuery Form 上傳,contentType 只能為 text/html,因為如果是 application/json 類型,IE8/9 會以文件下載的方式展現 json 數據。