asp上传文件到服务器的代码(asp上传文件到服务器的代码是多少)
- 服务器
- 2022-12-19 22:44:49
- 33
本篇文章给大家谈谈asp上传文件到服务器的代码,以及asp上传文件到服务器的代码是多少对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。
本文目录一览:
asp.net中文件如何上传到服务器上
{ } protected void Button1_Click(object sender, EventArgs e){if (this.filepost.PostedFile.FileName == ""){Response.Write("上传文件不能为空!");return;}try{string Path = Server.MapPath("upload/");//设置服务器端路径
string filePath = this.filepost.PostedFile.FileName;//获取客户端实际路径
string fileName = filePath.Substring(filePath.LastIndexOf("\\")+1);//获取文件名称
string serverPath = Path + fileName;//上传的文件保存到服务器端的路径
System.Text.StringBuilder buider = new System.Text.StringBuilder();//上传的文件信息:可变字符串
buider.Append("上传文件的类型:"+this.filepost.PostedFile.ContentType.ToString()+"");
buider.Append("客户端文件地址:"+this.filepost.PostedFile.FileName+"");
buider.Append("上传文件名称:"+fileName);
buider.Append("上传文件的扩展名:"+filePath.Substring(fileName.LastIndexOf(".")+1));
buider.Append("上传文件的大小:"+this.filepost.PostedFile.ContentLength/1024+"k"+"");
if (System.IO.File.Exists(serverPath)){Response.Write("这个文件在服务器上已经存在,请不要重复上传!");return;}string str = fileName.Substring(fileName.LastIndexOf(".")+1);//获取文件后缀名
if (str == "jpg" || str == "rar" || str == "ppt"){Response.Write("对不起,该类型文件不能被上传!");return;}if (this.filepost.PostedFile.ContentLength 2048){Response.Write("对不起,文件不能超过2K");return;}this.filepost.PostedFile.SaveAs(serverPath);//上传保存文件
asp如何实现文件上传功能
基本原理是:采用ADO Stream对象的BinaryRead方法将FORM中的所有数据读出,从中截取出所需的文件数据,以二进制文件方式存盘。
下面是上传文件页面的一个例子:
<html>
<body>
<form name="Upload" Method="Post" Enctype="multipart/form-data" Action="Upload.asp">
<input type="file" name="FileName">
<INPUT TYPE="Submit" VALUE="Upload"></TD>
</form>
</body>
</html>
扩展资料
几种文件上传技术的比较
1、基于HTTP协议
该方法需要编程者利用第三方软件,如DELPHI、VB等,在应用程序中先进行HTTP协议编程,然后将待上传文件内容按HTTP协议的格式打包,最后向WEB服务器发送上传的请求报文,从而实现文件的上传。
因为DELPHI和VB不能编写完整的WEB网络程序,只能编写WEB小应用程序,因此,该方法只用于功能受限的网络应用。
2、基于VB(或DELPHI等)开发的文件上传组件
该方法利用VB(或DELPHI等编程语言)开发ASP服务器组件,实现特定的文件上传服务。它首先利用ASP表单功能将文件(二进制格式)从用户端上传到服务器端,然后使用VB开发的组件,对二进制文件进行处理,成为可以正常读写的文件。
该方法要求编程者不仅掌握ASP语言,而且还能利用VB等第三方语言进行组件编程,增加了开发的难度。
3、基于数据库技术
该方法和上个方法有类似之处。不同的地方在于对上传的二进制文件的处理上。它使用数据库来保存二进制文件。无论是小型数据库还是大型数据库都提供了存储二进制数据的数据类型,只要以Append Chunk方式将数据存入相应的字段就可以了。
该方法虽然简单可行,但是因为每次上传的文件大小都是不一样的,因此,会对数据库的空间造成很大的浪费,降低了数据的访问速度;并且使得文件只能在数据库环境下进行访问,造成了很大的不便。
asp上传文件到服务器的代码怎么写?
html
head
meta http-equiv="Content-Type" content="text/html; charset=gb2312"
title无标题文档/title
/head
body
%
'On Error Resume Next
Response.Expires=0
if Request.TotalBytes then
set a=createobject("adodb.stream")
a.Type=1
a.Open
a.write Request.BinaryRead(Request.TotalBytes)
a.Position=0
b=a.Read
c=chrB(13)chrB(10)
d=clng(instrb(b,c))
e=instrb(d+1,b,c)
set f=createobject("adodb.stream")
f.type=1
f.open
a.Position=d+1
a.copyto f,e-d-3
f.Position=0
f.type=2
f.CharSet="GB2312"
g=f.readtext
f.Close
h=mid(g,instrRev(g,"\")+1,e)
i=instrb(b,cc)+4
j=instrb(i+1,b,leftB(b,d-1))-i-2
if j 1 then
set f =nothing
set a =nothing
response.write "未选择要上传的文件a href='?'重新上传/a"
response.end
end if
f.Type=1
f.Open
a.Position=i-1
a.CopyTo f,j
h = Mid(h, InStrRev(h, "filename=""") + 10) '这是我帮你添加的,文件名的获取没有正确
f.SaveToFile server.mappath("/EXCEL/" h),2
f.Close
set f=Nothing
a.Close
set a=Nothing
'response.write "a href="Server.URlEncode(h)""h"/a"
end if
If Err.number 0 Then
response.Write err.number
response.Write err.Description
Response.End
End If
%
script language="javascript"
function checkupload() {
if (document.upload_form.fe.value == "") {
alert("未选择要上传的文件");
return false;
}
}
/script
form name="upload_form" enctype="multipart/form-data" method="post" onsubmit="return(checkupload())"
input type="file" name="fe"/
input type="submit" value="上传" name="B1"/
/form
/body
/html
ASP上传文件代码
1、2可以解决,3有点复杂,我对无组件上传也不是很熟悉,很久没有碰过了,
建议你使用别人做好的上传类,像无惧上传类、稻香老农的化境asp无组件上传类等都可以,有很多,都挺好用的。
关于1、2的问题可参照下面的解决办法,因为你的代码全写在一行,读起来太麻烦,所以我给拆开了。代码如下:
html
head
meta http-equiv="Content-Type" content="text/html; charset=gb2312"
title无标题文档/title
/head
body
%
Response.Expires=0
if Request.TotalBytes then
set a=createobject("adodb.stream")
a.Type=1
a.Open
a.write Request.BinaryRead(Request.TotalBytes)
a.Position=0
b=a.Read
c=chrB(13)chrB(10)
d=clng(instrb(b,c))
e=instrb(d+1,b,c)
set f=createobject("adodb.stream")
f.type=1
f.open
a.Position=d+1
a.copyto f,e-d-3
f.Position=0
f.type=2
f.CharSet="GB2312"
g=f.readtext
f.Close
h=mid(g,instrRev(g,"\")+1,e)
i=instrb(b,cc)+4
j=instrb(i+1,b,leftB(b,d-1))-i-2
if j 1 then
set f =nothing
set a =nothing
response.write "未选择要上传的文件a href='?'重新上传/a"
response.end
end if
f.Type=1
f.Open
a.Position=i-1
a.CopyTo f,j
f.SaveToFile server.mappath("/images/" h),2 '上传至“/images/”文件夹中
f.Close
set f=Nothing
a.Close
set a=Nothing
response.write "a href="Server.URlEncode(h)""h"/a"
end if
%
script language="javascript"
function checkupload(){
if(document.upload_form.fe.value ==""){
alert("未选择要上传的文件");
return false;
}
}
/script
form name="upload_form" enctype="multipart/form-data" method="post" onsubmit="return(checkupload())"
input type="file" name="fe"
input type="submit" value="上传" name="B1"/form
/body
/html
asp上传文件到服务器的代码的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于asp上传文件到服务器的代码是多少、asp上传文件到服务器的代码的信息别忘了在本站进行查找喔。
本文由admin于2022-12-19发表在靑年PHP官网,如有疑问,请联系我们。
本文链接:https://www.qnphp.com/post/143888.html