有没有文件上传的源代码!最好是asp.net的?

lins1980 2002-05-27 10:08:30
有没有文件上传的源代码!最好是asp.net的?谢谢
...全文
140 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
wangfei2428 2002-05-28
  • 打赏
  • 举报
回复
asp.net本身就带有自动上传的功能,您看看书!
无须要!
xxjmz_78 2002-05-28
  • 打赏
  • 举报
回复
在浏览器中上载文件的方法与实现

--------------------------------------------------------------------------------


上海铁道大学
刘建莹 张宇
---- 一、问题引入

---- 在现在的管理信息系统中,比较先进的都已采用浏览器/服务器的模型,在这种模型中都要涉及到客户端与服务器端的信息交互问题,从服务器端到客户端的信息传递技术已经比较成熟,这里主要讨论从客户端到服务器端的文件上载问题,基于Microsoft的IE4.0、IIS4.0、ASP(Active Server Page)和标准HTML语言。

---- 二、实现方法

---- 在ASP页面中,利用HTML中的Form元素来实现。

---- 在Form元素的语法中,EncType表明提交数据的格式,Method表明提交的方法(Get/Post)。在IE4.0 及以后的版本中都支持"multipart/form-data"这种格式,相应的Method方法必须是Post,表明要上载文件到服务器。当然同时在服务器相应的WEB站点上要把属性设为可写。下面是一个示例:



< form enctype="multipart/form-data"
action="http://dev_d/upload/post/cpshost.dll?
PUBLISH?http://dev_d/upload/UserUploadAction.asp"
method=post id=form2 name=form2 >

1. Press the Browse button and
choose a File to upload from your computer.
< br >< input type="file" id=file1 name=file1 >
2. Upload the file.
< br >< input type=hidden size=80 name="TargetURL"
value=http://dev_d/upload/post >
< input type=submit value='Upload'
id=submit1 name=submit1 >
< /form >

---- 三、实现要素



1 Form的enctype="multipart/form-data"。

2 Form的action="(UserURL)/cpshost.dll?PUBLISH?
(UserURL)/UserUploadAction.asp"。

---- 说明:cpshost.dll是用于文件上载的动态链接文件,其后的PUBLISH参数也为固定,而(UserURL)指的是完整的URL地址,如:http://dev_d/upload。如果PUBLISH后没有参数,上载文件完成后,只是简单返回文件已经上载;如果PUBLISH后跟上完整URL的ASP文件,就可以用ASP来处理文件上载后的其他操作,如修改相应的数据库数据。可以在ASP文件中用Request.Form("Variable")来访问相应参数。对上载文件来说,Variable有四种可能的值:FileName 文件名称(不包括后缀),FileExtention 文件后缀(包括"."),FilePath 上载文件保存的服务器端路径,FileSize 上载文件的字节大小。

---- 3 Form的Method的方法必须为Post。

---- 4 Form中必须有一个input元素,而且input的属性type="file"。

---- 说明:如果要上载多个文件,有多个input元素就可以了,但至少有一个有效文件,否则会出错。

---- 系统会自动产生一个文本区域和一个"browse..."按钮,可以直接在文本区域内输入文件路径名称,或按"browse..."按钮,从文件对话框中选择一个文件。

---- 5 Form中必须有一个隐含(即type=hidden)input元素,而且input的属性name="TargetURL",属性

---- value="(UserURL)",(UserURL)即为上载文件保存位置的URL地址。

---- 说明:文件保存位置的URL地址属性必须设为可写,否则会返回此URL地址没有写的权限。

---- 6 Form中必须有一个submit按钮,即input的属性type="submit",此按钮即为上载按钮。或者在其他相关事件中调用此Form的Submit方法。但两种方法实际上本质相同,只不过用方法调用还可以在上载前加上其它处理代码,如数据的有效性检查等。

---- 四、完整实例

---- 1 用户上载文件页面UserUpload.asp



< % response.expires=0 % >
< HTML >
< HEAD >
< META NAME="GENERATOR"
Content="Microsoft Visual Studio 6.0" >
< /HEAD >

< BODY >

< form enctype="multipart/form-data"
action="http://dev_d/upload/post/cpshost.dll?
PUBLISH?http://dev_d/upload/UserUploadAction.asp"
method=post id=form2 name=form2 >
< table BORDER=0 CELLSPACING=3 CELLPADDING=3 >
< tr >
< td valign=top >< span >1. < /span >
< td >Press the Browse button and choose
a File to upload from your computer.
< br >< input type="file" id=file1 name=file1 >
< br >< input type="file" id=file2 name=file2 >
< /td >
< tr >
< TD vAlign=top >< SPAN >2. < /SPAN >
< TD >Upload the file.
< br >< input type=hidden size=80 name="TargetURL"
value="http://dev_d/upload/post" >
< input type=submit value='Upload'
id=submit1 name=submit1 >
< /td >
< /table >
< /form >
< /BODY >
< /HTML >

2 用户上载文件处理页面UserUploadAction.asp
< % Response.Buffer = TRUE % >
< % Response.expires=0 % >

< HTML >
< BODY >
< H3 >Upload Status:< BR >< /H3 >

< span style="color:gray" >< HR >
< % For I = 1 To Request.Form("FileName").Count
Response.Write "Uploaded File: < B >" &
Request.Form("FileName")(I) &
Request.Form("FileExtention")(I) &"< /B >< BR >"
Response.Write "Server Path: < B >" &
Request.Form("FilePath")(I) & "< /B >< BR >"
Response.Write "Size: < B >"
& Request.Form("FileSize")(I)
& " bytes< /B >< br >"
Next
FileName = Request.Form("FilePath")(1) &
Request.Form("FileName")(1) &
Request.Form("FileExtention")(1)
% >
< hr >< br >
< % if request.form("FilePath").count = 0 then
Response.Write ("No file was received.")
Response.End
else
Response.Write (filename+" File was received.")
end if % >
< /span >
< /BODY >
< /HTML >


xxjmz_78 2002-05-28
  • 打赏
  • 举报
回复
HTTP 方式上载文件的ASP实例

--------------------------------------------------------------------------------

太极计算机公司交通事业部
李 嘉


1. HTTP 方 式 的 文 件 上 载
---- 文 件 的 上 载( upload) 是 制 作 网 站 时 经 常 遇 到 的 问 题, 经 常 上 网 的 朋 友大 都 使 用 过WEB 方 式 的Email 发 信. 操 作 大 体 是 这 样: 当 选 择了 粘 贴 文 件 后, 直 接 输 入 本 地 文 件 的 绝 对 路 径 或 按BROWSER 按 钮 浏 览 要 粘 贴 的 文 件, 而 后 确 认. 在 这 个 过 程 中 实 际上 就 实 现 了HTTP 方 式 的 文 件 上 载.( 其 中BROWSER 按 钮 在 中 文版 的Netscape 和IE4 中 显 示 为” 浏 览..”). 这 里 文 件 从 客 户 端到 服 务 器 的 上 载 是 由HTTP 协 议 的 通 用 网 关 界 面(CGI) 支 持的. 这 种 上 载 方 式 要 求 浏 览 器 和WEB Server 两 方 面 都 能 够 支持Rfc1867. Netscape 的Web Server 和 浏 览 器 以 及IE4.0 都 支 持CGI 方 式 的上 载, 但 是 微 软 的Web Server IIS 还 不 能 提 供 这 样 的 功 能. 也就 是 说, 如 果 我 们 要 想 使 用ASP 实 现 文 件 上 载, 还 必 须 对IIS 进 行 配 置. 在 网 上 可 以 下 载 这 样 的Active Server Componet, 或 者用VB 等 开 发 工 具 为IIS 编 写 一 个 这 样 的Active Server Componet 来 支持Rfc1867. 由 于 采 用HTTP 方 式 上 载 文 件 对 使 用 者 来 说 是 最直 接 和 方 便 的, 相 信 大 家 对 下 面 的ASP 实 例 一 定 会 感 兴趣.



2. ASP 编 程 实 例
---- 为 了 使 大 家 对HTTP 方 式 的 上 载 有 一 个 感 性 的 认 识, 最 好 能 实 际 运 行 以 下这 个 例 子. 这 个 简 单 的 例 子 有 两 个 文 件. 一 个 是upload.html, 一 个 是up.asp. upload.html 中 有 一 个FORM, 它 有 两 个 按 钮” 浏 览..” 和” 上 载”, 用 户 在 文 本 框 中 输 入 文 件 在 本 地 的 绝 对 路径 或 通 过 浏 览 选 中 文 件 后 就 可 以 用” 上 载” 按 钮 将 文件 上 载. 而IIS 上 的up.asp 将 会 把 文 件 存 到 服 务 器 上 的 指 定路 径 下.

---- 2.1 upload.html 文 件



< HTML >
< HEAD >< TITLE > 请 上 载 文 件< /TITLE >< /HEAD >

< BODY >

< form enctype="multipart/form-data"
method="post" action="up.asp" >

输 入 上 载 文 件 的 名 称: < input type="file" name="f1" >< br >

< input type="submit" value=” 上 载” >

< /form >

< /BODY >

< /HTML >

2.2 up.asp 文 件
< %@ LANGUAGE="VBSCRIPT" % >

< HTML >< HEAD >

< TITLE > 上 载 文 件 测 试< /TITLE >

< /HEAD >

< BODY >

您 的 文 件 已 经 上 传 成 功!.< br >

< % Set upl = Server.CreateObject("SoftArtisans.FileUp") % >

< % upl.SaveAs "d:\upload\test.out" % >< BR >
文 件 长 度: < %=upl.TotalBytes% >
< /BODY >
< /HTML >


---- 2.3 例 子 说 明

---- 在HTML 中 FORM 必 须有 ENCTYPE="multipart/form-data".

---- < INPUT TYPE="FILE" > 也 是 必 须 的.

---- 当 对FORM 确 认 后, 浏 览 器 将 会 传 送 指 定 文 件 的 内 容.

---- 在ASP 中 实 际 上只 用 了 两 条 语 句 就 实 现 了 上 载

---- < % Set upl = Server.CreateObject("SoftArtisans.FileUp") % >

---- < % upl.SaveAs "d:\upload\test.out" % >

---- 第 一 条 语 句 使用 一 个SoftArtisans.FileUp 的 对 象 创 建 了 一 个 名 为 upl 的 实 例, 这 里 用 到 了 一 个 称 为SA-FileUp 的Active Server Componet. 第 二 条 语句 将 上 传 的 内 容 保 存 在 服 务 器 上.

---- 为 了 运 行 这 个例 子, 需 要 到http://www.serverpages.com/upload 站 点 下 载 这 个Active Server Componet, 并 用 它 配 置 微 软 的IIS 或Personal Web Server. 以 上 实 例 笔者 在IIS3.0 和I E4,Netsapce4.0 运 行 通 过.

林仪明 2002-05-27
  • 打赏
  • 举报
回复
功能:
1。把图片文件(JPG GIF PNG)上传,
2。保存到指定的路径(在web.config中设置路径,以文件的原有格式保存),
3。并自动生成指定宽度的(在web.config中设置宽度)
4。和指定格式的(在web.config中指定缩略图的格式)
5。和原图比例相同的缩略图(根据宽度和原图的宽和高计算所略图的高度)
6。可以判断是否已经存在文件
7。如果不覆盖,则给出错误
8。如果选中"覆盖原图"checkbox,则覆盖原图。
9。可以根据要求,在webform上设置1个以上的file input和相应的checkbox
10。并在文件上传完毕后,显示原图的文件名,尺寸,字节,和
11。缩略图的文件名尺寸。
12。缩略图的文件名格式:原图+"_thumb."+指定格式,如:test.jpg_thumb.gif,以便于管理。

--------------------
public void UploadFile(object sender, System.EventArgs e)
{

string imgNameOnly, imgNameNoExt, imgExt;
string imgThumbnail;
int erroNumber = 0;
System.Drawing.Image oriImg, newImg;

StringBuilder picInfo = new StringBuilder();

if(Page.IsValid)
{


for(int i = 0;i < Request.Files.Count; i++)
{
HttpPostedFile PostedFile = Request.Files[i];
string fileExt = (System.IO.Path.GetExtension(PostedFile.FileName)).ToString().ToLower();

imgNameOnly = System.IO.Path.GetFileName(PostedFile.FileName);
if(fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".png")
{

if(System.IO.File.Exists(ConfigurationSettings.AppSettings["FePicSavePath"]+ imgNameOnly) && (checkboxlistRewrite.Items[i].Selected == false))
{
erroNumber = erroNumber + 1;
picInfo.Append("<b>错误:</b>文件("+ (i+1) +") " + imgNameOnly + " 已经存在,请修改文件名<br>" );
}
}
else
{
erroNumber = erroNumber + 1;
picInfo.Append("<b>错误:</b>文件("+ (i+1) +") " + imgNameOnly + " 扩展名 " + fileExt + " 不被许可<br>" );
}

}

if(erroNumber > 0)
{
picInfo.Append("<font color=red>全部*作均未完成,请修改错误,再进行*作</font><br>");
}
else
{
for(int i = 0;i < Request.Files.Count; i++)
{

HttpPostedFile PostedFile = Request.Files[i];
imgNameOnly = System.IO.Path.GetFileName(PostedFile.FileName);
imgNameNoExt = System.IO.Path.GetFileNameWithoutExtension(PostedFile.FileName);
imgExt = System.IO.Path.GetExtension(PostedFile.FileName).ToString().ToLower();


oriImg = System.Drawing.Image.FromStream(PostedFile.InputStream);
newImg = oriImg.GetThumbnailImage(Int32.Parse(ConfigurationSettings.AppSettings["FePicThumbWidth"]),Int32.Parse(ConfigurationSettings.AppSettings["FePicThumbWidth"])*oriImg.Height/oriImg.Width,null,new System.IntPtr(0));
switch(imgExt)
{
//case ".jpeg":
case ".jpg":
oriImg.Save(ConfigurationSettings.AppSettings["FePicSavePath"] + imgNameOnly , System.Drawing.Imaging.ImageFormat.Jpeg);
break;
case ".gif":
oriImg.Save(ConfigurationSettings.AppSettings["FePicSavePath"] + imgNameOnly , System.Drawing.Imaging.ImageFormat.Gif);
break;
case ".png":
oriImg.Save(ConfigurationSettings.AppSettings["FePicSavePath"] + imgNameOnly , System.Drawing.Imaging.ImageFormat.Png);
break;
}

//oriImg.Save(ConfigurationSettings.AppSettings["FePicSavePath"] + imgNameNoExt + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);

switch(ConfigurationSettings.AppSettings["FePicThumbFormat"].ToString().ToLower())
{
//jpeg format can get the smallest file size, and the png is the largest size
//case "jpeg":
case "jpg":
newImg.Save(ConfigurationSettings.AppSettings["FePicSavePath"] + imgNameOnly + "_thumb.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
imgThumbnail = imgNameOnly + "_thumb.jpg";
break;
case "gif":
newImg.Save(ConfigurationSettings.AppSettings["FePicSavePath"] + imgNameOnly + "_thumb.gif",System.Drawing.Imaging.ImageFormat.Gif);
imgThumbnail = imgNameOnly + "_thumb.gif";
break;
case "png":
newImg.Save(ConfigurationSettings.AppSettings["FePicSavePath"] + imgNameOnly + "_thumb.png",System.Drawing.Imaging.ImageFormat.Png);
imgThumbnail = imgNameOnly + "_thumb.png";
break;
default:
newImg.Save(ConfigurationSettings.AppSettings["FePicSavePath"] + imgNameOnly + "_thumb.jpg",System.Drawing.Imaging.ImageFormat.Jpeg);
imgThumbnail = imgNameOnly + "_thumb.jpg";
break;

}//switch

picInfo.Append("<b>文件 名:</b>" + imgNameOnly + " ( " + oriImg.Width + " x " + oriImg.Height + " ) " + PostedFile.ContentLength/1024 + "KB<br>");
picInfo.Append("<b>缩略图名:</b>" + imgThumbnail + " ( " + newImg.Width + " x " + newImg.Height + " )<br><br>");

oriImg.Dispose();
newImg.Dispose();

}//for
picInfo.Append("<font color=red>所有*作成功</font><br>");

}// if erronumber = 0



}
else
{
picInfo.Append("<font color=red>有错误,请检查。*作未成功</font><br>");

}

lblPicInfo.Text = picInfo.ToString();

}

ibadboy 2002-05-27
  • 打赏
  • 举报
回复
你在vs.net的帮助中找upload,一堆例子
孟子E章 2002-05-27
  • 打赏
  • 举报
回复
asp.net自带了上传功能吧,可以直接使用
julyclyde 2002-05-27
  • 打赏
  • 举报
回复
真懒
.NET的没有,ASP的一大堆。看专题吧
saucer 2002-05-27
  • 打赏
  • 举报
回复
Uploading Files with ASP.NET
http://www.aspheute.com/english/20000802.asp

28,391

社区成员

发帖
与我相关
我的任务
社区描述
ASP即Active Server Pages,是Microsoft公司开发的服务器端脚本环境。
社区管理员
  • ASP
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧