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

上傳預覽

發布時間: 2022-01-08 09:58:47

Ⅰ 怎麼實現圖片上傳前預覽功能呢

預覽圖片
預覽功能的基本設計思路:創建一個img元素,再把文件域的value值賦值給img元素的src屬性。

<form name="form4" id="form4" method="post" action="#">

<input type="file" name="file4" id="file4" ōnchange="preview4()" />

<img id="pic4" src="" alt="圖片在此顯示" width="120"/>
</form>

<scrīpt type="text/javascrīpt">

function preview4(){

var x = document.getElementById("file4");

var y = document.getElementById("pic4");

if(!x || !x.value || !y)

return;

var patn = /\.jpg$|\.jpeg$|\.gif$/i;

if(patn.test(x.value)){

y.src = "file://localhost/" + x.value;

}

else{ alert("您選擇的似乎不是圖像文件。"); }

}

</scrīpt>

試下效果:


如果你用的是Firefox(或Opera),可能會發現什麼也沒有發生。是的,很不幸Firefox的安全策略不允許我們顯示一個用戶的本地圖像文件。不知道他們為什麼要這么做,我個人覺得圖像文件並不會造成嚴重的安全性問題。即使是不久前比較熱門的那個會引起Windows崩潰的jpeg文件,要顯示它的前提條件是用戶自己選擇了這個文件或者你知道這個文件在用戶硬碟上的准確路徑。所以我想這種策略很可能來自於一個「懶惰」的開發人員,他並不想多寫一些程序來區分這個本地文件是一個圖像文件還是一個惡意文件,Firefox對安全性的要求讓他們有些過於敏感了。

讓Firefox顯示本地文件的唯一辦法就是修改它的默認安全策略:

在Firefox的地址欄中輸入「about:config」
繼續輸入「security.checkloari」
雙擊下面列出來的一行文字,把它的值由true改為false
然後你可以再試試上面預覽,everything works well!可惜的是我們並不能要求所有的用戶都去修改這個值(更不用說修改的過程還挺麻煩),所以這對我們來說毫無意義。我們能做的也許就是接受Firefox不能預覽本地圖片這種「可笑」的局面。

用DOM來創建對象
在上面的XHTML代碼中,我們為了預覽圖片,事先加入了一個沒有設置src的img對象。除去不美觀、代碼冗餘之外,如果用戶瀏覽器不支持Javascrīpt,他不僅無法使用這個功能,還要接受頁面上一個永遠不會顯示出來的破圖。要解決這個問題,我們就需要在「運行時」再生成這個img對象,途徑還是DOM。

<form name="form5" id="form5" method="post" action="#">

<input type="file" name="file5" id="file5" ōnchange="preview5()"/>

</form>

<scrīpt type="text/javascrīpt">

function preview5(){

var x = document.getElementById("file5");

if(!x || !x.value)

return;

var patn = /\.jpg$|\.jpeg$|\.gif$/i;

if(patn.test(x.value)){

var y = document.getElementById("img5");

if(y){ y.src = 'file://localhost/' + x.value; }

else{

var img=document.createElement('img');

img.setAttribute('src','file://localhost/'+x.value);

img.setAttribute('width','120');

img.setAttribute('height','90');

img.setAttribute('id','img5');

document.getElementById('form5').appendChild(img);

}

}

else{ alert("您選擇的似乎不是圖像文件。"); }

}

</scrīpt>

Ⅱ 在用input上傳圖片時如何在旁邊預覽

核心思想是: 圖片上傳成功後,本頁面保持不刷新。
核心技巧是: form target

代碼:

<iframe id='hydro_emergency_team_uploadPhoto_if' style='display:none' name='send'><html><body>x</body></html></iframe>
<form id='xx' method='post' enctype='multipart/form-data' target='send' action="upload.php">
<input type="file" name="file" />
<input type="submit"/>
</form>

這段代碼表示: 表單提交後,本頁面不會刷新,僅僅是刷新 iframe 而已

Ⅲ java 怎麼實現在線預覽上傳的word等文件的預覽功能

可以將word先轉為pdf,然後在頁面中嵌一個iframe,在這個frame中直接打開這個pdf文件。
僅供參考

Ⅳ 怎麼用jquery或者javascript實現:如圖1的那個按鈕,可以讓上傳或預覽圖片

1、點擊圖1的那個按鈕可以讓用戶選擇上傳的圖片,選擇了一張就在按鈕前面預覽,如果用戶不想上傳某張圖片可以點擊x取消,再點擊上傳按鈕可以接著選擇圖片上傳。

2、設置選擇圖片的按鈕的 click 事件,去觸發 #upload_file 的 click 事件,然後就可以點擊圖片按鈕後,選擇本地圖片文件了。

3、設置上傳按鈕,其實一般手機版網頁是沒有上傳按鈕的,即選擇圖片後就通過 Ajax 上傳圖片了。

Ⅳ php 如何實現圖片上傳前預覽,並且有多個圖片上傳和預覽

<form name="form4" id="form4" method="post" action="#"> <input type="file" name="file4" id="file4" ōnchange="preview4()" /> <img id="pic4" src="" alt="圖片在此顯示" width="120"/> </form> <scrīpt type="text/javascrīpt"> function preview4(){ var x = document.getElementById("file4"); var y = document.getElementById("pic4"); if(!x || !x.value || !y) return; var patn = /\.jpg$|\.jpeg$|\.gif$/i; if(patn.test(x.value)){ y.src = "file://localhost/" + x.value; } else{ alert("您選擇的似乎不是圖像文件。"); } } </scrīpt>

Ⅵ 我需要一個js或者jquery能批量上傳圖片+預覽的功能。急~~~急~~~急~~

WebUploader項目,符合你的要求。

//文件上傳過程中創建進度條實時顯示。
uploader.on('uploadProgress',function(file,percentage){
var$li=$('#'+file.id),
$percent=$li.find('.progressspan');

//避免重復創建
if(!$percent.length){
$percent=$('<pclass="progress"><span></span></p>')
.appendTo($li)
.find('span');
}

$percent.css('width',percentage*100+'%');
});
//文件上傳成功,給item添加成功class,用樣式標記上傳成功。
uploader.on('uploadSuccess',function(file){
$('#'+file.id).addClass('upload-state-done');
});

//文件上傳失敗,顯示上傳出錯。
uploader.on('uploadError',function(file){
var$li=$('#'+file.id),
$error=$li.find('div.error');

//避免重復創建
if(!$error.length){
$error=$('<divclass="error"></div>').appendTo($li);
}

$error.text('上傳失敗');
});

//完成上傳完了,成功或者失敗,先刪除進度條。
uploader.on('uploadComplete',function(file){
$('#'+file.id).find('.progress').remove();
});

更多細節,請查看js源碼

Ⅶ javascript裡面需要上傳視頻,如何做到本地預覽視頻

<input type="file" accept="video/*" onChange={this.previewVideo} />

<videoid="myVideo"autoPlaywidth="300"/>

使用FileReader讀取轉為Data URL:

previewVideo=(file)=>{
//建議判斷下視頻大小及格式,太大的可能會有問題
constreader=newFileReader();
reader.onload=(evt)=>{
constmyVideo=document.querySelector("#myVideo");
myVideo.src=evt.target.result;
};
reader.readAsDataURL(file);
}

Ⅷ ASP.NET圖片上傳預覽

<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<span style="font-size: 12px; color: Red;">*</span>
<asp:Button ID="Button1" runat="server" Text="上傳" OnClick="Button1_Click" /><span style="color:Red; font-size:12px;">多圖上傳(三張)</span>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
</asp:UpdatePanel>
</div

protected void Button1_Click(object sender, EventArgs e)
{
string fileurl = UpFiles(FileUpload1);
Session["propic"] = Session["propic"] + fileurl + ",";

///顯示圖片
string str = Session["propic"].ToString();
string[] temp = str.Split(new string[] { "," }, StringSplitOptions.RemoveEmptyEntries);
for (int i = 0; i < temp.Length; i++)
{
Image img = new Image();
img.ID = "img" + i;
img.ImageUrl = "~/upfiles/" + temp[i];
img.Width = 50;
img.Height = 50;
img.BorderWidth = 1;
UpdatePanel1.ContentTemplateContainer.Controls.Add(img);
}
}

/// <summary>
/// 上傳
/// </summary>
/// <param name="fileuploadname"></param>
/// <returns></returns>
public string UpFiles(FileUpload fileuploadname)
{
string fullname = fileuploadname.FileName.ToString();//這個屬性是以前2003沒有的,也許是我沒注意看,反正現在我才第一次用!直接取得文件名
string url = fileuploadname.PostedFile.FileName.ToString();//這個是以前2003用的,先取得全部的上傳文件路徑個名字,然後再利用SubString方法來得到用戶名,現在看來是沒有必要了
string typ = fileuploadname.PostedFile.ContentType.ToString();//獲取文件MIME內容類型
string typ2 = fullname.Substring(fullname.LastIndexOf(".") + 1);//獲取文件名字 . 後面的字元作為文件類型
string size = fileuploadname.PostedFile.ContentLength.ToString();
string filename = "";
//下面是保存了,我們來個判斷,只能上穿傳那些文件吧
if (typ2 == "gif" || typ2 == "jpg" || typ2 == "bmp" || typ2 == "png")
{
filename = fullname;
fileuploadname.SaveAs(Server.MapPath("~/upfiles") + "\\" + filename);//將文件保存在跟目錄的UP文件夾
}
else
{
Response.Write("<script>alert('文件類型不正確!');document.location.href='add.aspx'</script>");
}
return filename;
}

Ⅸ 我上傳圖片時,網頁顯示不了上傳的預覽

會不會是文件太大了,選擇100kb左右的jpg格式的試試。

Ⅹ php圖片上傳前預覽怎麼實現!!

1.先創建一個file表單域,我們需要用它來瀏覽本地文件。
<form name="form1" id="form1" method="post" action="upload.php">
<input type="file" name="file1" id="file1" />
</form>
2.試下效果:
判斷文件類型:
當用戶選擇了一個圖片文件時,希望他能馬上看到這張圖片的縮略圖,以便他能確認沒有把自己的光屁股照片當作頭像傳到伺服器上^_^。
在預覽之前還得先判斷一下用戶選擇的是不是一個圖像文件,如果他想用一個.rar文件做頭像的話我們也需要禮貌地提醒一下。
<form name="form2" id="form2" method="post" action="upload.php">
<input type="file" name="file2" id="file2"
onchange="preview()" />
</form>
Javascript函數實現,注意使用DOM方法getElementById來訪問對象。不要再使用form
和input的name屬性來訪問對象了,只有IE才這么干。<script type="text/javascript">
function preview2(){
var x = document.getElementById("file2");
if(!x || !x.value) return;
if(x.value.indexOf(".jpg")<0
&& x.value.indexOf(".jpeg")<0
&& x.value.indexOf(".gif")<0){
alert("您選擇的似乎不是圖像文件。");
}else{
alert("通過");
}
}
</script>
3.試下效果:

這里有一個問題,如果用戶選擇了名為「fake.jpg.txt」的文件,這段腳本仍舊會認為這是一個合法的圖像文件。一個可行的解決方案是先 把文件名轉換成小寫,再取文件路徑的最後4到5位,判斷一下文件的擴展名是否確為支持的圖像文件擴展名。不過這種方案略顯笨拙,也沒有什麼美感可言, 我們換一種方案:用「正則表達式」來判斷文件擴展名。
<script type="text/javascript">
function preview3(){
var x = document.getElementById("file3");
if(!x || !x.value) return;
var patn = /\.jpg$|\.jpeg$|\.gif$/i;
if(patn.test(x.value)){
alert("通過");
}else{
alert("您選擇的似乎不是圖像文件。");
}
}
</script>
4.看看效果(可以自己創建一個「fake.jpg.txt」文件試試):

回到這段腳本上來,即使你還看不懂正則表達式那兩行,但整段腳本的美感還是很明顯的:簡潔、直接、語義流暢,這與Web標准關於XHTML的要求是一致的,與Web設計師或開發者天生的「完美」主義也是一致的。
jjww一大段之後,轉入重點——預覽圖片
預覽功能的基本設計思路是很清晰的:創建一個img元素,再把文件域的value值賦值給img
元素的src屬性。<form name="form4" id="form4" method="post" action="#">
<input type="file" name="file4" id="file4"
onchange="preview4()" />
<img id="pic4" src="http://blog.163.com/lgh_2002/blog/" alt="圖片在此顯示" width="120"/>
</form>
<script type="text/javascript">
function preview4(){
var x = document.getElementById("file4");
var y = document.getElementById("pic4");
if(!x || !x.value || !y) return;
var patn = /\.jpg$|\.jpeg$|\.gif$/i;
if(patn.test(x.value)){
y.src = "file://localhost/" + x.value;
}else{
alert("您選擇的似乎不是圖像文件。");
}
}
</script>
5.試下效果:

如果用的是Firefox(或Opera),可能會發現什麼也沒有發生。是的,很不幸Firefox的安全策略不允許顯示一個用戶的本地 圖像文件。不知道為什麼要這么做,個人覺得圖像文件並不會造成嚴重的安全性問題。即使是不久前比較熱門的那個會引起Windows崩潰的jpeg文 件,要顯示它的前提條件是用戶自己選擇了這個文件或者你知道這個文件在用戶硬碟上的准確路徑。所以我想這種策略很可能來自於一個「懶惰」的開發人員,並 不想多寫一些程序來區分這個本地文件是一個圖像文件還是一個惡意文件,Firefox對安全性的要求讓他們有些過於敏感了。
讓Firefox顯示本地文件的唯一辦法就是修改它的默認安全策略:
在Firefox的地址欄中輸入「about:config」
繼續輸入「security.checkloari」
雙擊下面列出來的一行文字,把它的值由true改為false
然後你可以再試試上面預覽,everything works well!可惜的是並不能要求所有的用戶都去修改這個值(更不用說修改的過程還挺麻煩),所以毫無意義。我們能做的也許就是接受Firefox不能預覽本地圖片這種「可笑」的局面。
用DOM來創建對象
在上面的XHTML代碼中,為了預覽圖片,事先加入了一個沒有設置src的img對象。除去不美觀、代碼冗餘之外,如果用戶瀏覽器不支持 Javascript,不僅無法使用這個功能,還要接受頁面上一個永遠不會顯示出來的破圖。要解決這個問題,就需要在「運行時」再生成這個img對 象,途徑還是DOM。
<form name="form5" id="form5" method="post" action="#">
<input type="file" name="file5" id="file5"
onchange="preview5()"/>
</form>
<script type="text/javascript">
function preview5(){
var x = document.getElementById("file5");
if(!x || !x.value) return;
var patn = /\.jpg$|\.jpeg$|\.gif$/i;
if(patn.test(x.value)){
var y = document.getElementById("img5");
if(y){
y.src = 'file://localhost/' + x.value;
}else{
var img=document.createElement('img');
img.setAttribute('src','file://localhost/'+x.value);
img.setAttribute('width','120');
img.setAttribute('height','90');
img.setAttribute('id','img5');
document.getElementById('form5').appendChild(img);
}
}else{
alert("您選擇的似乎不是圖像文件。");
}
}
</script>
6.試下效果:

這樣就相對比較完美了。DOM和正則表達式一樣,都是「包你不悔」的實 用技術,如果你希望更多了解、深入學習、或者順利實踐Web標准,DOM是不可或缺的。從本人最近的體會來說,Javascript+DOM+CSS蘊 藏著強大的能量,就看怎麼釋放它了。

7.最後帖上JQUERY的上傳預覽代碼:

de><html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="http://blog.163.com/lgh_2002/blog/jquery.js"></script>
<script language="javascript">
$(function(){
var ei = $("#large");
ei.hide();
$("#img1").mousemove(function(e){
ei.css({top:e.pageY,left:e.pageX}).html('<img style="border:1px solid gray;" src="http://blog.163.com/lgh_2002/blog/' + this.src + '" />').show();
}).mouseout( function(){
ei.hide("slow");
})
$("#f1").change(function(){
$("#img1").attr("src","file:///"+$("#f1").val());
})
});
</script>
<style type="text/css">
#large{position:absolute;display:none;z-index:999;}
</style>
</head>
<body>
<form name="form1" id="form1">
<div id="demo">
<input id="f1" name="f1" type="file" />
<img id="img1" width="60" height="60">
</div>
<div id="large"></div>
</form>
</body>
</html>de>

熱點內容
單片機android 發布:2024-09-20 09:07:24 瀏覽:762
如何提高三星a7安卓版本 發布:2024-09-20 08:42:35 瀏覽:661
如何更換伺服器網站 發布:2024-09-20 08:42:34 瀏覽:309
子彈演算法 發布:2024-09-20 08:41:55 瀏覽:286
手機版網易我的世界伺服器推薦 發布:2024-09-20 08:41:52 瀏覽:815
安卓x7怎麼邊打游戲邊看視頻 發布:2024-09-20 08:41:52 瀏覽:160
sql資料庫安全 發布:2024-09-20 08:31:32 瀏覽:91
蘋果連接id伺服器出錯是怎麼回事 發布:2024-09-20 08:01:07 瀏覽:505
編程鍵是什麼 發布:2024-09-20 07:52:47 瀏覽:655
學考密碼重置要求的證件是什麼 發布:2024-09-20 07:19:46 瀏覽:479