當前位置:首頁 » 文件傳輸 » 上傳圖片代碼
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

上傳圖片代碼

發布時間: 2022-01-12 09:54:37

1. 請問,上傳圖片的源代碼怎麼寫還有,怎麼上傳本地圖片

文章里貼圖有兩種方法: 1,網路圖片鏈接:在文章編輯時,點擊上面功能區中的「插入圖片」在對話框中粘貼網路圖片地址,確定即可顯示。鉤選顯示源代碼即可看到「圖片的源代碼」。 2,本地上傳圖片:編輯文章時,點擊下面的「圖片剪貼板」—瀏覽—找到你計算機中准備好的圖片後,選中確定,然後隨游標插入圖片即可。需要注意的是本地上傳圖片要求一定是「jpg和gif格式的,容量不能超過200KB」。 上面的兩種方法都可以使用,而且鉤選顯示源代碼,都可以看到圖片添加的代碼,復制下來就可以了。

2. 在html頁面裡面製作一個上傳圖片的區域,就是點那個區域,就能上傳照片,這個html代碼應該怎麼寫

你好,html上傳文件的代碼如下:

<inputtype="file"name="fileUpload"/>
<inputtype="submit"value="上傳文件"/>

至於你要說的有個區域顯示圖片的,這個需要配合前端開發才可以,單純的html代碼是做不到的,謝謝。

3. 圖片上傳的代碼

<%@ language="javascript"%>
<%
var self = Request.serverVariables("SCRIPT_NAME");
if (Request.serverVariables("REQUEST_METHOD")=="POST")
{
var oo = new uploadFile();
oo.path = "myFile"; //存放路徑,為空表示當前路徑,默認為uploadFile
oo.named = "file"; //命名方式,date表示用日期來命名,file表示用文件名本身,默認為file
oo.ext = "all"; //允許上傳的擴展名,all表示都允許,默認為all
oo.over = true; //當存在相同文件名時是否覆蓋,默認為false
oo.size = 1*1024*1024; //最大位元組數限制,默認為1G
oo.upload();
Response.write('<script type="text/javascript">location.replace("'+self+'")</script>');
}

//ASP無組件上傳類
function uploadFile()
{
var bLen = Request.totalBytes;
var bText = Request.binaryRead(bLen);
var oo = Server.createObject("ADODB.Stream");
oo.mode = 3;
this.path = "uploadFile";
this.named = "file";
this.ext = "all";
this.over = false;
this.size = 1*1024*1024*1024; //1GB

//文件上傳
this.upload = function ()
{
var o = this.getInfo();
if (o.size>this.size)
{
alert("文件過大,不能上傳!");
return;
}
var f = this.getFileName();
var ext = f.replace(/^.+\./,"");
if (this.ext!="all"&&!new RegExp(this.ext.replace(/,/g,"|"),"ig").test(ext))
{
alert("目前暫不支持擴展名為 "+ext+" 的文件上傳!");
return;
}
if (this.named=="date")
{
f = new Date().toLocaleString().replace(/\D/g,"") + "." + ext;
}

oo.open();
oo.type = 1;
oo.write(o.bin);
this.path = this.path.replace(/[^\/\\]$/,"$&/");
var fso = Server.createObject("Scripting.FileSystemObject");
if(this.path!=""&&!fso.folderExists(Server.mapPath(this.path)))
{
fso.createFolder(Server.mapPath(this.path));
}
try
{
oo.saveToFile(Server.mapPath(this.path+f),this.over?2:1);
alert("上傳成功!");
}
catch(e)
{
alert("對不起,此文件已存在!");
}
oo.close();
delete(oo);

}

//獲取二進制和文件位元組數
this.getInfo = function ()
{
oo.open();
oo.type=1;
oo.write(bText);
oo.position = 0;
oo.type=2;
oo.charset="unicode";
var gbCode=escape(oo.readText()).replace(/%u(..)(..)/g,"%$2%$1");
var sPos=gbCode.indexOf("%0D%0A%0D%0A")+12;
var sLength=bLen-(gbCode.substring(0,gbCode.indexOf("%0D%0A")).length/3)-sPos/3-6;
oo.close();

oo.open();
oo.type = 1;
oo.write(bText);
oo.position=sPos/3;
var bFile=oo.read(sLength);
oo.close();

return { bin:bFile, size:sLength };
}

//獲取文件名
this.getFileName = function ()
{
oo.open();
oo.type = 2;
oo.writeText(bText);
oo.position = 0;
oo.charset = "gb2312";
var fileName = oo.readText().match(/filename=\"(.+?)\"/i)[1].split("\\").slice(-1)[0];
oo.close();
return fileName;
}

function alert(msg)
{
Response.write('<script type="text/javascript">alert("'+msg+'");</script>');
}
}
%>
<html>
<head>
<title>ASP無組件上傳類</title>
<meta http-equiv="content-Type" content="text/html; charset=gb2312">
</head>
<body>
<form action="<%=self%>" method="post" enctype="multipart/form-data" onSubmit="return (this.upFile.value!='');">
<input type="file" name="upFile"/>
<input type="submit" value="上傳文件"/>
</form>
</body>
</html>

4. 上傳圖片代碼修改

<%
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="木目,Woodeye" />
<meta name="description" content="木目ASP文件上傳工具" />
<meta name="keywords" content="木目,ASP,Upload,文件上傳" />
<style type="text/css">
<!--
body,input {font-size:12px;}
-->
</style>
<title>vip圖片上傳小於100k</title>
</head>
<body id="body">
<%
ExtName = "jpg,gif,png" '允許擴展名
SavePath = "Upload_img" '保存路徑
If Right(SavePath,1)<>"/" Then SavePath=SavePath&"/" '在目錄後加(/)
CheckAndCreateFolder(SavePath)

UpLoadAll_a = Request.TotalBytes '取得客戶端全部內容
If(UpLoadAll_a>0) Then
If(UpLoadAll_a<=1024*100) Then'=======================此處限制圖片大小小於100KB=========================
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><br />"
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 "文件上傳: <span style=""color:red;"">" & SaveFileName & " </span>"<html><head><meta http-equiv='Refresh' content='3 url=""javascript:window.close();""'></head><body><center><br><br>文件上傳成功<br>本窗口三秒後自動關閉</center></body></html>"<a href=""" & Request.ServerVariables("URL") &"""></a><br />"
End If
Else
Response.write "文件上傳: <span style=""color:red;"">文件格式不正確!</span> <a href=""" & Request.ServerVariables("URL") &""">重新上傳文件</a><br />"
End If

Else
'=====================此處為提示文件大小超過了=========================
Response.write "上傳文件過大,小於100kb! <a href='javascript:history.back()'>返回</a>"
End If
Else
%>
<script language="Javascript">
<!--
function ValidInput()
{

if(document.upform.upfile.value=="")
{
alert("請選擇上傳文件!")
document.upform.upfile.focus()
return false
}
return true
}
// -->
</script>
<form action='<%= Request.ServerVariables("URL") %>' method='post' name="upform" onsubmit="return ValidInput()" enctype="multipart/form-data">
文件上傳:
<input type='file' name='upfile' size="40" />
<input type='submit' value="上傳">
</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
%>