欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

jquery異步上傳,jq同步請(qǐng)求與異步請(qǐng)求

jQuery異步提交表單的兩種方式

本文為大家分享了兩種jQuery異步提交表單的方式,具體內(nèi)容如下

目前成都創(chuàng)新互聯(lián)已為近1000家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)絡(luò)空間、網(wǎng)站托管、服務(wù)器租用、企業(yè)網(wǎng)站設(shè)計(jì)、巴馬網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長(zhǎng),共同發(fā)展。

第一種方式:普通ajax方式提交

$(function(){

$('#send').click(function(){

$.ajax({

type:

"GET",

url:

GLOBAL_PATH

+

"/Enterprise/savecompanyphoto",

data:

{username:$("#username").val(),

content:$("#content").val()},

dataType:

"json",

success:

function(data){

$('#resText').empty();

//清空resText里面的所有內(nèi)容

var

html

=

'';

$.each(data,

function(commentIndex,

comment){

html

+=

'div

class="comment"h6'

+

comment['username']

+

':/h6p

class="para"'

+

comment['content']

+

'/p/div';

});

$('#resText').html(html);

}

});

});

});

第二種方式:普通ajaxSubmit方式提交表單

script

src="jquery.form.js"

type="text/javascript"/script

script

src="dialog.js?lib=false"

type="text/javascript"/scriptsrc="jquery.min.js"

type="text/javascript"

function

uploader_img(){

var

optionsSave={

type:

"POST",

url:

GLOBAL_PATH

+

"/Enterprise/savecompanyphoto",

data:$('#addImg').serialize(),

success:

function

(data)

{

if

(data.code

==

0)

{

AlertMini('alt1',

"上傳圖片成功!",

'success.gif',

2);

window.location.reload();

}

else

{

AlertMini('alt1',

"上傳圖片失敗!",

'error.gif',

2);

}

},

error:

function

(data)

{

AlertMini('alt1',

"上傳圖片失敗!",

'error.gif',

2);

}

}

$('#addImg').ajaxSubmit(optionsSave);

}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

jQuery插件之a(chǎn)jaxFileUpload

ajaxFileUpload是一個(gè)異步上傳文件的jQuery插件,語(yǔ)法:$.ajaxFileUpload([options])。

使用方法:

第一步:先引入jQuery與ajaxFileUpload插件。注意先后順序。

script src="jquery-1.7.1.js" type="text/javascript"/script

script src="ajaxfileupload.js" type="text/javascript"/script

第二步:HTML代碼

 第三步:JS代碼第四步:后臺(tái)頁(yè)面upload.aspx代碼。

jsp中使用jquery的ajaxfileupload插件怎么實(shí)現(xiàn)異步上傳

ajaxfileupload實(shí)現(xiàn)異步上傳的完整例子:

JSP頁(yè)面中引入的script代碼:

script

function ajaxFileUpload()

{

$("#loading").ajaxStart(function(){

$(this).show();

})//開(kāi)始上傳文件時(shí)顯示一個(gè)圖片

.ajaxComplete(function(){

$(this).hide();

});//文件上傳完成將圖片隱藏起來(lái)

$.ajaxFileUpload({

url:'AjaxImageUploadAction.action',//用于文件上傳的服務(wù)器端請(qǐng)求地址

secureuri:false,//一般設(shè)置為false

fileElementId:'imgfile',//文件上傳空間的id屬性 input type="file" id="imgfile" name="file" /

dataType: 'json',//返回值類型 一般設(shè)置為json

success: function (data, status) //服務(wù)器成功響應(yīng)處理函數(shù)

{

alert(data.message);//從服務(wù)器返回的json中取出message中的數(shù)據(jù),其中message為在struts2中定義的成員變量

if(typeof(data.error) != 'undefined')

{

if(data.error != '')

{

alert(data.error);

}else

{

alert(data.message);

}

}

},

error: function (data, status, e)//服務(wù)器響應(yīng)失敗處理函數(shù)

{

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;

}*/

/**

* 檢測(cè)上傳文件的擴(kuò)展名是否合法

* */

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;

}

}

怎么樣通過(guò)jQuery Ajax實(shí)現(xiàn)上傳文件

Query Ajax在web應(yīng)用開(kāi)發(fā)中很常用,它主要包括有ajax,get,post,load,getscript等等這幾種常用無(wú)刷新操作方法,接下來(lái)通過(guò)本文給大家介紹jquery ajax 上傳文件處理方式。

FormData對(duì)象

XMLHttpRequest Level 2添加了一個(gè)新的接口FormData.利用FormData對(duì)象,我們可以通過(guò)JavaScript用一些鍵值對(duì)來(lái)模擬一系列表單控件,我們還可以使用XMLHttpRequest的send()方法來(lái)異步的提交這個(gè)”表單”.比起普通的ajax,使用FormData的最大優(yōu)點(diǎn)就是我們可以異步上傳一個(gè)二進(jìn)制文件.

所有主流瀏覽器的較新版本都已經(jīng)支持這個(gè)對(duì)象了,比如Chrome 7+、Firefox 4+、IE 10+、Opera 12+、Safari 5+。之前都是用原生js的XMLHttpRequest寫的請(qǐng)求

XMLHttpRequest方式

xhr.open("POST", uri, true);

xhr.onreadystatechange = function() {

if (xhr.readyState == 4 xhr.status == 200) {

// Handle response.

alert(xhr.responseText); // handle response.

}

};

fd.append('myFile', file);

// Initiate a multipart/form-data upload

xhr.send(fd);

其實(shí)jquery的ajax也可以支持到的,關(guān)鍵是設(shè)置:processData 和 contentType 。

ajax方式

var formData = new FormData();

var name = $("input").val();

formData.append("file",$("#upload")[0].files[0]);

formData.append("name",name);

$.ajax({

url : Url,

type : 'POST',

data : formData,

// 告訴jQuery不要去處理發(fā)送的數(shù)據(jù)

processData : false,

// 告訴jQuery不要去設(shè)置Content-Type請(qǐng)求頭

contentType : false,

beforeSend:function(){

console.log("正在進(jìn)行,請(qǐng)稍候");

},

success : function(responseStr) {

if(responseStr.status===0){

console.log("成功"+responseStr);

}else{

console.log("失敗");

}

},

error : function(responseStr) {

console.log("error");

}

});

求一段JS或Jquery異步上傳圖片的代碼

圖片和文件等流媒體 上傳都是靠from表單的提交。

你可以設(shè)置一個(gè)隱藏的from表單

里面有個(gè)input id='file' type='file'

選擇玩圖片之后賦值給file

然后用jquery from表單提交即可

form?id="form"?runat="server"?enctype="multipart/form-data"?

input?id='file'?type='file'

/from

$.ajax({

url:'XXXX',//上傳后臺(tái)路徑

data:$('#form').serialize(),

type:"POST",

success:function(){

}

});

當(dāng)前題目:jquery異步上傳,jq同步請(qǐng)求與異步請(qǐng)求
網(wǎng)址分享:http://chinadenli.net/article48/dsepsep.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站商城網(wǎng)站全網(wǎng)營(yíng)銷推廣微信小程序手機(jī)網(wǎng)站建設(shè)服務(wù)器托管

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

成都app開(kāi)發(fā)公司