當前位置:首頁 » 文件管理 » iframeajax上傳文件

iframeajax上傳文件

發布時間: 2022-06-04 22:06:04

1. 怎麼用ajax上傳文件,通俗的講 怎麼在得到input file 的文件 不是路徑,,而且這個文件。流

ajax.post是不能上傳文件的。非同步上傳文件比較流行的是一般是兩種思路。
一、利用flash,很多jquery插件都是利用flash
二、利用iframe。

2. ajax怎麼提交帶文件上傳表單

上傳的文件是沒有辦法和表單內容一起非同步的,可考慮使用jquery的ajaxfileupload,或是其他的插件,非同步上傳文件後,然後再對表單進行操作。

3. struts2 如何通過ajax上傳文件

ajax是不能上傳文件的,一般做法是使用一個隱藏的iframe 來個傳,達到無刷新上傳的效果。
還有就是使用swf上傳控制項,swfUpload等

4. 實現Iframe內文件的上傳

我知道你的意思,你是想實現那種所謂的「無刷新上傳文件」吧
呵呵。。。
給你個思路,在頁面內部上傳文件你應該是會的,這個就不用說了,就是加了iframe之後需要在外部得到上傳文件的路徑,然後保存到伺服器,這個可能是你不知道怎麼弄的問題吧,(我是猜的,因為既然你能提出這樣的問題,應該說明不加iframe的本頁上傳你是會的)

你應該這樣做,這個按鈕寫在iframe內部,就用它實現上傳,直接上傳,然後需要在iframe的外部調用它的頁面得到路徑
類似a.html中有個<iframe src="b.aspx"></iframe>
1、把按鈕寫在b.aspx,並且定義一個隱藏域,暫定為<input type="hidden" id="filePath_hidden" runat="server" />
這個隱藏域可以由後台賦值
2、點擊按鈕,用你的C#代碼上傳文件,並且將路徑保存到filePath_hidden裡面,filePath_hidden.value=路徑;
剩下的,我寫起來比較費時間,而且代碼比較多,但是不復雜,相信你一看就知道了,得到b.aspx內部filePath_hidden的值就好辦了
具體看到http://58.51.84.58:81裡面去,注冊一個會員,然後進入會員中心,修改資料那裡,我就是這么實現的,內部有個iframe,指向上傳文件的文件,在外部如何保存數據的,在會員中心那個頁面你可以查看源代碼,看看,如果將iframe內部的數據返回給外部。得到iframe內部的數據,在外部就可以像平時那樣保存數據了,
聲明,這個網站只是我給別的公司做的一個企業網站,因為我在這里寫的比較多,你可以直接去參考,不是做廣告,那個公司跟我們公司沒有任何關系

既然你說用asp.net回答,那麼我就告訴你,那隻有一種辦法,就是在內部上傳之後用session保存(Cookie也可以)
session["路徑"]=路徑;
之後在外部頁面調用這個session值就可以了,

5. extjs ajax 可以上傳文件嗎

文件上傳的Ajax,首先Ajax並不支持流的傳輸,只是在裡面套了個iframe。

//ajax上傳
Ext.get('btn').on('click',function(){
Ext.Ajax.request({
url:'Upload.php',
isUpload:true,
form:'upform',
success:function(){
Ext.Msg.alert('upfile','文件上傳成功!');
}
});
});
<formid="upform">
請選擇文件:<inputtype="file"name="imgFile"/>
<inputtype="button"id="btn"value="上傳"/>
</form>
<?php
if(!isset($_FILES['imgFile'])){
echojson_encode(array("success"=>false,'msg'=>"NotgetImgfile"));
return;
}
$upfile=$_FILES['imgFile'];
$name=$upfile["name"];//上傳文件的文件名
$type=$upfile["type"];//上傳文件的類型
$size=$upfile["size"];//上傳文件的大小
$tmp_name=$upfile["tmp_name"];//上傳文件的臨時存放路徑
$error_cod=$upfile["error"];
if($error_cod>0){
echojson_encode(array("success"=>false,'msg'=>$error_cod));
}
$photo_tmp_file_name=//這里設置存放路徑
move_uploaded_file($tmp_name,$photo_tmp_file_name);//存儲文件
?>

6. ajax上傳文件提交時,enctype=multipart/form-data怎麼帶過去

form中的欄位,加上get set方法

private FormFile file;

private String filename;

private String filesize;

action 部分:

public ActionForward execute(ActionMapping mapping, ActionForm form,

HttpServletRequest request, HttpServletResponse response)

throws Exception {

String dir="D:/";

UpFileForm uff=(UpFileForm)form;

FormFile file=uff.getFile();

if(file.getFileSize()==0){

return mapping.findForward("success");

}

String fname=file.getFileName();

String size=Integer.toString(file.getFileSize())+"bytes";

InputStream streamIn=file.getInputStream();

OutputStream streamOut=new FileOutputStream(dir+"/"+fname);

int bytesRead=0;

byte[] buffer=new byte[8192];

while((bytesRead=streamIn.read(buffer,0,8192))!=-1){

streamOut.write(buffer,0,bytesRead);

}

streamOut.close();

streamIn.close();

uff.setFilename(fname);

uff.setFilesize(size);

file.destroy();

return mapping.findForward("success");

}

這樣將上傳的文件存在d盤。

7. 前端上傳文件的幾種方法

1.表單上傳

最傳統的圖片上傳方式是form表單上傳,使用form表單的input[type=」file」]控制項,打開系統的文件選擇對話框,從而達到選擇文件並上傳的目的。

form表單上傳

表單上傳需要注意以下幾點:

(1).提供form表單,method必須是post。

(2).form表單的enctype必須是multipart/form-data。

javascript學習交流群:453833554

enctype 屬性規定在發送到伺服器之前應該如何對表單數據進行編碼。默認地,表單數據會編碼為 "application/x-www-form-urlencoded"。就是說,在發送到伺服器之前,所有字元都會進行編碼。HTML表單如何打包數據文件是由enctype這個屬性決定的。enctype有以下幾種取值:

application/x-www-form-urlencoded:在發送前編碼所有字元(默認)(空格被編碼為』+』,特殊字元被編碼為ASCII十六進制字元)。

multipart/form-data:不對字元編碼。在使用包含文件上傳控制項的表單時,必須使用該值。

text/plain:空格轉換為 「+」 加號,但不對特殊字元編碼。

默認enctype=application/x-www-form-urlencoded,所以表單的內容會按URL規則編碼,然後根據表單的提交方法:

method=』get』 編碼後的表單內容附加在請求連接後,

method=』post』 編碼後的表單內容作為post請求的正文內容。

8. 怎麼用ajax 提交上傳的文件上傳後在界面顯示出來。但界面不刷新

1. 頁面加入隱藏iframe,假如name=xxxx
2. form的target設置成xxxx
3. 後台上傳圖片後,向前端輸出一個函數如:parent.yourfunc('圖片url')
4. 頁面上定義yourfunc函數,參數為url
5. 圖片上傳完成後,會調用yourfunc函數
6. 在yourfunc中拿到url,下步你該知道怎麼做了吧

9. 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的全局變數值。然後再做其它處理。

10. 如何用ajax上傳文件

  1. 引入ajaxfileupload.js

jQuery.extend({


createUploadIframe:function(id,uri)
{
//createframe
varframeId='jUploadFrame'+id;

if(window.ActiveXObject){
vario=document.createElement('<iframeid="'+frameId+'"name="'+frameId+'"/>');
if(typeofuri=='boolean'){
io.src='javascript:false';
}
elseif(typeofuri=='string'){
io.src=uri;
}
}
else{
vario=document.createElement('iframe');
io.id=frameId;
io.name=frameId;
}
io.style.position='absolute';
io.style.top='-1000px';
io.style.left='-1000px';

document.body.appendChild(io);

returnio
},
createUploadForm:function(id,fileElementId)
{
//createform
varformId='jUploadForm'+id;
varfileId='jUploadFile'+id;
varform=$('<formaction=""method="POST"name="'+formId+'"id="'+formId+'"enctype="multipart/form-data"></form>');
varoldElement=$('#'+fileElementId);
varnewElement=$(oldElement).clone();
$(oldElement).attr('id',fileId);
$(oldElement).before(newElement);
$(oldElement).appendTo(form);
//setattributes
$(form).css('position','absolute');
$(form).css('top','-1200px');
$(form).css('left','-1200px');
$(form).appendTo('body');
returnform;
},
addOtherRequestsToForm:function(form,data)
{
//addextraparameter
varoriginalElement=$('<inputtype="hidden"name=""value="">');
for(varkeyindata){
name=key;
value=data[key];
varcloneElement=originalElement.clone();
cloneElement.attr({'name':name,'value':value});
$(cloneElement).appendTo(form);
}
returnform;
},

ajaxFileUpload:function(s){
//TODOintroceglobalsettings,,notonlytimeout
s=jQuery.extend({},jQuery.ajaxSettings,s);
varid=newDate().getTime()
varform=jQuery.createUploadForm(id,s.fileElementId);
if(s.data)form=jQuery.addOtherRequestsToForm(form,s.data);
vario=jQuery.createUploadIframe(id,s.secureuri);
varframeId='jUploadFrame'+id;
varformId='jUploadForm'+id;
//Watchforanewsetofrequests
if(s.global&&!jQuery.active++)
{
jQuery.event.trigger("ajaxStart");
}
varrequestDone=false;
//Createtherequestobject
varxml={}
if(s.global)
jQuery.event.trigger("ajaxSend",[xml,s]);
//Waitforaresponsetocomeback
varuploadCallback=function(isTimeout)
{
vario=document.getElementById(frameId);
try
{
if(io.contentWindow)
{
xml.responseText=io.contentWindow.document.body?io.contentWindow.document.body.innerHTML:null;
xml.responseXML=io.contentWindow.document.XMLDocument?io.contentWindow.document.XMLDocument:io.contentWindow.document;

}elseif(io.contentDocument)
{
xml.responseText=io.contentDocument.document.body?io.contentDocument.document.body.innerHTML:null;
xml.responseXML=io.contentDocument.document.XMLDocument?io.contentDocument.document.XMLDocument:io.contentDocument.document;
}
}catch(e)
{
jQuery.handleError(s,xml,null,e);
}
if(xml||isTimeout=="timeout")
{
requestDone=true;
varstatus;
try{
status=isTimeout!="timeout"?"success":"error";
//
if(status!="error")
{
//processthedata()
vardata=jQuery.uploadHttpData(xml,s.dataType);
//Ifalocalcallbackwasspecified,fireitandpassitthedata
if(s.success)
s.success(data,status);

//Firetheglobalcallback
if(s.global)
jQuery.event.trigger("ajaxSuccess",[xml,s]);
}else
jQuery.handleError(s,xml,status);
}catch(e)
{
status="error";
jQuery.handleError(s,xml,status,e);
}

//Therequestwascompleted
if(s.global)
jQuery.event.trigger("ajaxComplete",[xml,s]);

//HandletheglobalAJAXcounter
if(s.global&&!--jQuery.active)
jQuery.event.trigger("ajaxStop");

//Processresult
if(s.complete)
s.complete(xml,status);

jQuery(io).unbind()

setTimeout(function()
{ try
{
$(io).remove();
$(form).remove();

}catch(e)
{
jQuery.handleError(s,xml,null,e);
}

},100)

xml=null

}
}
//Timeoutchecker
if(s.timeout>0)
{
setTimeout(function(){
//
if(!requestDone)uploadCallback("timeout");
},s.timeout);
}
try
{
//vario=$('#'+frameId);
varform=$('#'+formId);
$(form).attr('action',s.url);
$(form).attr('method','POST');
$(form).attr('target',frameId);
if(form.encoding)
{
form.encoding='multipart/form-data';
}
else
{
form.enctype='multipart/form-data';
}
$(form).submit();

}catch(e)
{
jQuery.handleError(s,xml,null,e);
}
if(window.attachEvent){
document.getElementById(frameId).attachEvent('onload',uploadCallback);
}
else{
document.getElementById(frameId).addEventListener('load',uploadCallback,false);
}
return{abort:function(){}};

},

uploadHttpData:function(r,type){
vardata=!type;
data=type=="xml"||data?r.responseXML:r.responseText;
//Ifthetypeis"script",evalitinglobalcontext
if(type=="script")
jQuery.globalEval(data);
//GettheJavaScriptobject,ifJSONisused.
if(type=="json")
{
//,
//youhavetodeletethe'<pre></pre>'tag.
//ThepretaginChromehasattribute,sohavetouseregextoremove
vardata=r.responseText;
varrx=newRegExp("<pre.*?>(.*?)</pre>","i");
varam=rx.exec(data);
//thisisthedesireddataextracted
vardata=(am)?am[1]:"";//theonlysubmatchorempty
eval("data="+data);
}
//evaluatescriptswithinhtml
if(type=="html")
jQuery("<div>").html(data).evalScripts();
//alert($('param',data).each(function(){alert($(this).attr('value'));}));
returndata;
}
})

2.引入上傳文件所需的jar

7.獲取之後怎麼處理自己看著辦咯,我只能幫到這里了

熱點內容
db2新建資料庫 發布:2024-09-08 08:10:19 瀏覽:170
頻率計源碼 發布:2024-09-08 07:40:26 瀏覽:778
奧迪a6哪個配置帶後排加熱 發布:2024-09-08 07:06:32 瀏覽:100
linux修改apache埠 發布:2024-09-08 07:05:49 瀏覽:208
有多少個不同的密碼子 發布:2024-09-08 07:00:46 瀏覽:566
linux搭建mysql伺服器配置 發布:2024-09-08 06:50:02 瀏覽:995
加上www不能訪問 發布:2024-09-08 06:39:52 瀏覽:811
銀行支付密碼器怎麼用 發布:2024-09-08 06:39:52 瀏覽:513
蘋果手機清理瀏覽器緩存怎麼清理緩存 發布:2024-09-08 06:31:32 瀏覽:554
雲伺服器的優點與缺點 發布:2024-09-08 06:30:34 瀏覽:734