當前位置:首頁 » 文件管理 » js實現圖片上傳

js實現圖片上傳

發布時間: 2022-01-08 20:34:57

A. 求js或JQ代碼,實現圖片上傳後在指定的div中以背景圖顯示

如果是background的話使用 $("div").css("background-image","圖片地址"),如果使用的img的話使用$("div").attr("src","地址");關鍵就是獲取圖片地址給弄上去

B. js/jquery如何實現一張圖片的上傳預覽功能。。。。

使用AJAX,把文件保存到臨時目錄,把返回的值(文件路徑),放到頁面上預先添加的<IMG> 標簽中,就可顯示預覽圖片,再點上傳才把文件從臨時目錄(刪除臨時目錄文件)復制到你指定的目錄中

C. 用javascript寫一個圖片上傳的功能,將圖片放置在一個文件夾

用ASP做吧!一共三個頁面。再建一個名為photo.mdb資料庫!' index.asp<%
Response.Buffer = True
Server.ScriptTimeOut=9999999
On Error Resume Next
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns=" http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<meta http-equiv="Content-Language" content="zh-cn" />
<meta content="all" name="robots" />
<meta name="author" content="mhooo,Woodeye" />
<style type="text/css">
<!--
body,input {font-size:12px;}
-->
</style>
<script language="JavaScript">
<!--
//圖片按比例縮放
var flag=false;
function DrawImage(ImgD){
var image=new Image();
var iwidth = 420; //定義允許圖片寬度
var iheight = 390; //定義允許圖片高度
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= iwidth/iheight){
if(image.width>iwidth){
ImgD.width=iwidth;
ImgD.height=(image.height*iwidth)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>iheight){
ImgD.height=iheight;
ImgD.width=(image.width*iheight)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
}
}
//-->
</script>
<title></title>
</head>
<body id="body">
<div align="center">
<%
ExtName = "jpg,gif,png" '允許擴展名
SavePath = "upload" '保存路徑
If Right(SavePath,1)<>"/" Then SavePath=SavePath&"/" '在目錄後加(/)
CheckAndCreateFolder(SavePath) UpLoadAll_a = Request.TotalBytes '取得客戶端全部內容
If(UpLoadAll_a>0) Then
Set UploadStream_c = Server.CreateObject("ADODB.Stream")
UploadStream_c.Type = 1
UploadStream_c.Open
UploadStream_c.Write Request.BinaryRead(UpLoadAll_a)
UploadStream_c.Position = 0 FormDataAll_d = UploadStream_c.Read
CrLf_e = chrB(13)&chrB(10)
FormStart_f = InStrB(FormDataAll_d,CrLf_e)
FormEnd_g = InStrB(FormStart_f+1,FormDataAll_d,CrLf_e) Set FormStream_h = Server.Createobject("ADODB.Stream")
FormStream_h.Type = 1
FormStream_h.Open
UploadStream_c.Position = FormStart_f + 1
UploadStream_c.CopyTo FormStream_h,FormEnd_g-FormStart_f-3
FormStream_h.Position = 0
FormStream_h.Type = 2
FormStream_h.CharSet = "GB2312"
FormStreamText_i = FormStream_h.Readtext
FormStream_h.Close FileName_j = Mid(FormStreamText_i,InstrRev(FormStreamText_i,"\")+1,FormEnd_g) If(CheckFileExt(FileName_j,ExtName)) Then
SaveFile = Server.MapPath(SavePath & FileName_j) If Err Then
Response.Write "文件上傳: <span style=""color:red;"">文件上傳出錯!</span> <a href=""" & Request.ServerVariables("URL") &""">重新上傳文件</a>
"
Err.Clear
Else
SaveFile = CheckFileExists(SaveFile) k=Instrb(FormDataAll_d,CrLf_e&CrLf_e)+4
l=Instrb(k+1,FormDataAll_d,leftB(FormDataAll_d,FormStart_f-1))-k-2
FormStream_h.Type=1
FormStream_h.Open
UploadStream_c.Position=k-1
UploadStream_c.CopyTo FormStream_h,l
FormStream_h.SaveToFile SaveFile,2 SaveFileName = Mid(SaveFile,InstrRev(SaveFile,"\")+1)
Response.write "文件上傳: <img src=../img.asp?src=" & SaveFileName & " onload ='DrawImage(this)'/> 文件上傳成功! <a href=""" & Request.ServerVariables("URL") &""">繼續上傳文件</a><p><span style=""color:red;"">(申請將圖片顯示在網站首頁!)</span> <form action='a.asp' method='post'><input name='pic' type='hidden' value='" & SaveFileName & "' /><input type='submit' name='Submit' value='點擊申請' style='height:18px;border:1px solid #CCCCCC'/></form>"
End If
Else
Response.write "文件上傳: <span style=""color:red;"">文件格式不正確!</span> <a href=""" & Request.ServerVariables("URL") &""">重新上傳文件</a>
"
End If Else
%>
<script language="Javascript">
<!--
function ValidInput()
{ if(document.upform.upfile.value=="")
{
alert("請選擇上傳文件!")
document.upform.upfile.focus()
return false
}
return true
}
// -->
</script>
</div>
<form action='<%= Request.ServerVariables("URL") %>' method='post' name="upform" onsubmit="return ValidInput()" enctype="multipart/form-data">
文件上傳:
<input type='file' name='upfile' size="40" style="height:18px;border:1px solid #CCCCCC" > <input type='submit' value="上傳" style="height:18px;border:1px solid #CCCCCC">
</form>
<%
End if
Set FormStream_h = Nothing
UploadStream.Close
Set UploadStream = Nothing
%>
</body>
</html>
<%
'判斷文件類型是否合格
Function CheckFileExt(FileName,ExtName) '文件名,允許上傳文件類型
FileType = ExtName
FileType = Split(FileType,",")
For i = 0 To Ubound(FileType)
If LCase(Right(FileName,3)) = LCase(FileType(i)) then
CheckFileExt = True
Exit Function
Else
CheckFileExt = False
End if
Next
End Function '檢查上傳文件夾是否存在,不存在則創建文件夾
Function CheckAndCreateFolder(FolderName)
fldr = Server.Mappath(FolderName)
Set fso = CreateObject("Scripting.FileSystemObject")
If Not fso.FolderExists(fldr) Then
fso.CreateFolder(fldr)
End If
Set fso = Nothing
End Function '檢查文件是否存在,重命名存在文件
Function CheckFileExists(FileName)
Set fso=Server.CreateObject("Scripting.FileSystemObject")
If fso.FileExists(SaveFile) Then
i=1
msg=True
Do While msg
CheckFileExists = Replace(SaveFile,Right(SaveFile,4),"_" & i & Right(SaveFile,4))
If not fso.FileExists(CheckFileExists) Then
msg=False
End If
i=i+1
Loop
Else
CheckFileExists = FileName
End If
Set fso=Nothing
End Function
%> ' a.asp<!--#include file="conn.asp" -->
<%
pic=request.form("pic")
exec="insert into guest(pic)values('"+pic+"')"
conn.execute exec
conn.close
set conn=nothing
Response.Write("<script language=javascript>alert('恭喜您,申請成功!您上傳的圖片將在本站首頁顯示!')</script>")
response.write("<script>window.opener=null;window.close();</script>")
%>
'conn.asp<%
set conn=server.createobject("adodb.connection")
conn.open "driver={microsoft access driver (*.mdb)};dbq="&server.mappath("data/photo.mdb")
%>

D. 怎樣用js或者jq實現點擊這個圖片就可以選擇上傳還有預覽圖片啊

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Document</title>
<script src="jquery-3.1.1.min.js"></script>
</head>
<body>
<h3>請選擇圖片文件:JPG/GIF</h3>
<form name="form0" id="form0" >
<input type="file" name="file0" id="file0" multiple="multiple" />
<br><br><img src="" id="img0" width="120">
</form>
</body>
<script>
$("#file0").change(function(){
var objUrl = getObjectURL(this.files[0]) ;
console.log("objUrl = "+objUrl) ;
if (objUrl)
{
$("#img0").attr("src", objUrl);
$("#img0").removeClass("hide");
}
}) ;
//建立一個可存取到該file的url
function getObjectURL(file)
{
var url = null ;
if (window.createObjectURL!=undefined)
{ // basic
url = window.createObjectURL(file) ;
}
else if (window.URL!=undefined)
{
// mozilla(firefox)
url = window.URL.createObjectURL(file) ;
}
else if (window.webkitURL!=undefined) {
// webkit or chrome
url = window.webkitURL.createObjectURL(file) ;
}
return url ;
}
$('input').on('change',function(){
var value = $(this).val();
value = value.split("\\")[2];
alert(value);
})
</script>
</html>

E. js關於圖片上傳路徑的獲取

瀏覽器安全性已經大大提高,要實現圖片上傳預覽不是那麼簡單了

不過有很多變通或先進的方法來實現

例如ie7/ie8的濾鏡預覽法,firefox3的getAsDataURL方法

具體可以參考這個圖片上傳預覽效果

F. 我想實現 html +js 上傳圖片 並保存到本地tmp目錄下,現有代碼如下,求指導。必採納

你js代碼把文件以base64編碼形式展示了出來,是為了讓用戶上傳文件之前能夠預覽對吧。


文件的IO操作需要用後端來實現,如果你只是做web前端開發的話,就沒有必要研究這個東西,如果你是後端開發者的話可以嘗試一下,相關的資料很多,我寫個示例吧,後端用php為例:

html實現:

<!DOCTYPEhtml>
<html>
<head>
<metacharset="utf-8">
<title>ss</title>
</head>
<body>
<formaction="file.php"method="post"enctype="multipart/form-data">
<inputtype="file"name="upfile">
<inputtype="submit"value="提交">
</form>
</body>
</html>

php實現(file.php):

<?php
@header('Content-Type:text/html;charset=utf-8');
if(!isset($_FILES['upfile'])){
exit('請選擇您要上傳的文件!');
}

if(!file_exists($_FILES['upfile']['tmp_name'])){
exit('您要上傳的文件不存在!');
}

$file_dir=dirname(__FILE__).'/tmp';
if(!is_file($file_dir)){
@mkdir($file_dir,0777,true);
}

$file_ext='.jpg';
if(preg_match('/(.w+)$/',$_FILES['upfile']['name'],$ext_tmp)){
$file_ext=$ext_tmp[1];
}

$file_save_path=$file_dir.'/'.uniqid().mt_rand(101,999).$file_ext;

@rename($_FILES['upfile']['tmp_name'],$file_save_path);

if(!file_exists($file_save_path)){
exit('文件上傳失敗!');
}

exit('文件上傳成功!');

G. 用js、jquery如何實現上傳圖片的預覽

$("#btnLoadPhoto").upload({ url: "../UploadForms/RequestUpload.aspx?action=photo", type: "json", callback: calla });
//獲得表單元素
HttpPostedFile oFile = context.Request.Files[0];
//設置上傳路徑
string strUploadPath = "temp/";
//獲取文件名稱
string fileName = context.Request.Files[0].FileName;
補充:JQuery是繼prototype之後又一個優秀的Javascript庫。它是輕量級的js庫 ,它兼容CSS3,還兼容各種瀏覽器(IE 6.0+, FF 1.5+, Safari 2.0+, Opera 9.0+),jQuery2.0及後續版本將不再支持IE6/7/8瀏覽器。jQuery使用戶能更方便地處理HTML(標准通用標記語言下的一個應用)、events、實現動畫效果,並且方便地為網站提供AJAX交互。jQuery還有一個比較大的優勢是,它的文檔說明很全,而且各種應用也說得很詳細,同時還有許多成熟的插件可供選擇。jQuery能夠使用戶的html頁面保持代碼和html內容分離,也就是說,不用再在html裡面插入一堆js來調用命令了,只需要定義id即可。

H. 用js腳本,怎麼實現彈出選擇圖片的對話框,並將文件上傳到伺服器

彈出框用框架(layer.js,具體使用可參考官方文檔)。

HTML如下:

!!! 注意這種提交會刷新頁面

<formname="upload-file"method="post"enctype="multipart/form-data">
<labelfor="upload-file">上傳</label>
<inputid="upload-file"type="file"name="images"hidden="hidden">
<inputtype="submit"value="提交">
</form>

提交過後後端就可以獲取到圖片(PHP獲取代碼如下:$_POST['images'];)

I. 如何用javascript實現<img>控制項的圖片上傳

fileupload(前台id:fleupld)控制項加一個onclick方法:getvalue()
{document.getelementbyid("label的id").innerhtml=document.getelementbyid("fleupld").value;}
document.getelementbyid("fleupld").value
獲取的是fileupload中文件的全路徑,當然你可以截取這個值,獲得其子字元串(單個文件的名稱)。

J. 前端,js實現圖片上傳的原理是設么能回答面試即可

H5的話,就是把本地的圖片按照指定的格式讀取到緩存里,再供JS代碼進行調用傳給後台,格式的話base64吧

熱點內容
紅米4怎麼關閉密碼鎖屏密碼 發布:2024-05-20 18:55:00 瀏覽:766
買手機看哪些參數配置圖片 發布:2024-05-20 18:35:02 瀏覽:7
右鍵文件夾未響應 發布:2024-05-20 18:34:23 瀏覽:986
汽車安卓軟體在哪裡下載 發布:2024-05-20 18:33:38 瀏覽:886
編程智能積木 發布:2024-05-20 18:26:03 瀏覽:160
命星速演算法 發布:2024-05-20 18:26:02 瀏覽:942
python的u 發布:2024-05-20 18:24:25 瀏覽:133
電信網關的密碼是什麼 發布:2024-05-20 18:17:22 瀏覽:470
twitter安卓怎麼更新版本 發布:2024-05-20 18:17:14 瀏覽:923
c語言常量變數 發布:2024-05-20 18:12:22 瀏覽:910