---
title: "asp简单文件上传源码"
author: "coolmaple"
date: "2007-06-18T17:45:44Z"
tags: []
slug: "asp"
draft: false
lastmod: "2007-06-18T17:45:44Z"
id: 91652
url: "https://coolmaple.iteye.com/blog/91652"
---

**文件1.上传界面文件   upload.htm**

&lt;html&gt;

&lt;head&gt;  
&lt;meta http-equiv="Content-Language" content="zh-cn"&gt;  
&lt;meta http-equiv="Content-Type" content="text/html; charset=gb2312"&gt;  
&lt;title&gt;文件上传&lt;/title&gt;  
&lt;/head&gt;

&lt;body&gt;  
&lt;form method="POST" action="SaveFile.asp"&gt;  
文件上传：&lt;input type="file" name="file" size="42"&gt;&nbsp;&lt;input type="submit" value="提交" name="bb"&gt;  
&lt;/form&gt;

&lt;/body&gt;

&lt;/html&gt;

**文件2.保存上传文件   SaveFile.asp**

&lt;%  
dim file,filename,houzui  
file = Request.Form("file")

if file="" then  
 response.write"&lt;script&gt;alert('请选择要上传的文件！');window.location.href='upload.htm';&lt;/script&gt;"  
else  
 houzui=mid(file,InStrRev(file, "."))  
   
 if houzui=".gif" or houzui=".jpg" or houzui=".bmp" then   '允许上传的文件类型  
  filename=year(date) & month(date) & day(date) & Hour(time) & minute(time) & second(time) & houzui

  Set objStream = Server.CreateObject("ADODB.Stream")  
  objStream.Type = 1  
  objStream.Open  
  objStream.LoadFromFile file  
  objStream.SaveToFile Server.MapPath(filename),2  
  objStream.Close

//============================把文件名写入数据库，如无需要，可删除此段代码！  
  Set conn = Server.CreateObject("ADODB.Connection")  
  conn.open "DRIVER={Microsoft Access Driver (\*.mdb)}; DBQ=" & Server.MapPath("mb.mdb")  
  set rs=server.CreateObject("adodb.recordset")  
  rs.open "select\*from img",conn,1,2  
  rs.addnew  
  rs("name")=filename  
  rs.update  
  set rs=nothing  
  conn.close  
  set conn=nothing  
//========================================

  response.write"&lt;script&gt;alert('图片上传成功！');window.location.href='upload.htm';&lt;/script&gt;"  
 else  
  response.write"&lt;script&gt;alert('不允许上传" & houzui & "的格式！');window.location.href='upload.htm';&lt;/script&gt;"  
 end if  
end if  
%&gt;