asp文件上传:文件上传 源代码
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了asp文件上传:文件上传 源代码相关的知识,希望对你有一定的参考价值。
参考技术A<! #include FILE="upload inc" > <% dim upload file formName formPath iCount filename fileExt set upload=new upload_ xSoft 建立上传对象 formPath="uploadimages/" 在目录后加(/) if right(formPath )<>"/" then formPath=formPath&"/" iCount= for each formName in upload file 列出所有上传了的文件 set file=upload file(formName) 生成一个文件对象 if file filesize< then response write "<font size= ><br>请先选择你要上传的图片 [ <a href=# onclick=history go( )>重新上传</a> ]</font>" response end end if if file filesize> then response write "<font size= ><br>图片大小超过了限制 [ <a href=# onclick=history go( )>重新上传</a> ]</font>" response end end if
fileExt=lcase(right(file filename ))
if fileEXT<>" jpg" and fileEXT<>" gif" then response write "<font size= ><br>文件格式只能为jpg和gif格式 [ <a href=# onclick=history go( )>重新上传</a> ]</font>" response end end if randomize ranNum=int( *rnd)+ filename=formPath&year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&fileExt filename =year(now)&month(now)&day(now)&hour(now)&minute(now)&second(now)&fileExt if file FileSize> then 如果 FileSize > 说明有文件数据 file SaveAs Server mappath(filename) 保存文件 response write "<script>parent form img value= "&FileName &" </script>"
iCount=iCount+ end if set file=nothing next set upload=nothing 删除此对象
Response Write "<img src= "&" /UpLoad/UpLoadImages/"&FileName &" onload = DrawImage(this) >" response end %>
下面是upload inc的代码
<SCRIPT RUNAT=SERVER LANGUAGE=VBSCRIPT> dim upfile_ xSoft_Stream Class upload_ xSoft dim Form File Version Private Sub Class_Initialize dim iStart iFileNameStart iFileNameEnd iEnd vbEnter iFormStart iFormEnd theFile dim strDiv mFormName mFormValue mFileName mFileSize mFilePath iDivLen mStr if Request TotalBytes< then Exit Sub set Form=CreateObject("Scripting Dictionary") set File=CreateObject("Scripting Dictionary") set upfile_ xSoft_Stream=CreateObject("Adodb Stream") upfile_ xSoft_Stream mode= upfile_ xSoft_Stream type= upfile_ xSoft_Stream open upfile_ xSoft_Stream write Request BinaryRead(Request TotalBytes) vbEnter=Chr( )&Chr( ) iDivLen=inString( vbEnter)+ strDiv=subString( iDivLen) iFormStart=iDivLen iFormEnd=inString(iformStart strDiv) while iFormStart < iFormEnd iStart=inString(iFormStart "name=""") iEnd=inString(iStart+ """") mFormName=subString(iStart+ iEnd iStart ) iFileNameStart=inString(iEnd+ "filename=""") if iFileNameStart> and iFileNameStart<iFormEnd then iFileNameEnd=inString(iFileNameStart+ """") mFileName=subString(iFileNameStart+ iFileNameEnd iFileNameStart ) iStart=inString(iFileNameEnd+ vbEnter&vbEnter) iEnd=inString(iStart+ vbEnter&strDiv) if iEnd>iStart then mFileSize=iEnd iStart else mFileSize= end if set theFile=new FileInfo theFile FileName=getFileName(mFileName) theFile FilePath=getFilePath(mFileName) theFile FileSize=mFileSize theFile FileStart=iStart+ theFile FormName=FormName file add mFormName theFile else iStart=inString(iEnd+ vbEnter&vbEnter) iEnd=inString(iStart+ vbEnter&strDiv)
if iEnd>iStart then mFormValue=subString(iStart+ iEnd iStart ) else mFormValue="" end if form Add mFormName mFormValue end if
iFormStart=iformEnd+iDivLen iFormEnd=inString(iformStart strDiv) wend End Sub
Private Function subString(theStart theLen) dim i c stemp upfile_ xSoft_Stream Position=theStart stemp="" for i= to theLen if upfile_ xSoft_Stream EOS then Exit for c=ascB(upfile_ xSoft_Stream Read( )) If c > Then if upfile_ xSoft_Stream EOS then Exit for stemp=stemp&Chr(AscW(ChrB(AscB(upfile_ xSoft_Stream Read( )))&ChrB(c))) i=i+ else stemp=stemp&Chr(c) End If Next subString=stemp End function
Private Function inString(theStart varStr) dim i j bt theLen str InString= Str=toByte(varStr) theLen=LenB(Str) for i=theStart to upfile_ xSoft_Stream Size theLen if i>upfile_ xSoft_Stream size then exit Function upfile_ xSoft_Stream Position=i if AscB(upfile_ xSoft_Stream Read( ))=AscB(midB(Str )) then InString=i for j= to theLen if upfile_ xSoft_Stream EOS then inString= Exit for end if if AscB(upfile_ xSoft_Stream Read( ))<>AscB(MidB(Str j )) then InString= Exit For end if next if InString<> then Exit Function end if next End Function
Private Sub Class_Terminate form RemoveAll file RemoveAll set form=nothing set file=nothing upfile_ xSoft_Stream close set upfile_ xSoft_Stream=nothing End Sub Private function GetFilePath(FullPath) If FullPath <> "" Then GetFilePath = left(FullPath InStrRev(FullPath "")) Else GetFilePath = "" End If End function Private function GetFileName(FullPath) If FullPath <> "" Then GetFileName = mid(FullPath InStrRev(FullPath "")+ ) Else GetFileName = "" End If End function
Private function toByte(Str) dim i iCode c iLow iHigh toByte="" For i= To Len(Str) c=mid(Str i ) iCode =Asc(c) If iCode< Then iCode = iCode + If iCode> Then iLow = Left(Hex(Asc(c)) ) iHigh =Right(Hex(Asc(c)) ) toByte = toByte & chrB("&H"&iLow) & chrB("&H"&iHigh) Else toByte = toByte & chrB(AscB(c)) End If Next End function End Class
lishixinzhi/Article/program/net/201311/14198
Asp.Net上传大文件带进度条swfupload
Asp.Net基于swfupload上传大文件带进度条百分比显示,漂亮大气上档次,大文件无压力,先看效果
一、上传效果图
1、上传前界面:图片不喜欢可以自己换
2、上传中界面:百分比显示
3、上传后返回文件地址,我测试呢所以乱写的
二、核心代码
upload.htm代码
<!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 runat="server"> <title>博客园:webapi</title> <link href="css.css" rel="stylesheet" type="text/css" /> <script type="text/javascript" src="swfupload/swfupload.js"></script> <script type="text/javascript" src="js/swfupload.queue.js"></script> <script type="text/javascript" src="js/fileprogress.js"></script> <script type="text/javascript" src="js/filegroupprogress.js"></script> <script type="text/javascript" src="js/handlers.js"></script> <script type="text/javascript"> var swfu; window.onload = function () { var settings = { flash_url: "swfupload/swfupload.swf", upload_url: "uploadFile.ashx", file_size_limit: "409600", file_types: "*.apk;*.ipa", file_types_description: "Web Image Files", file_size_limit: "307200", file_upload_limit: 1, file_queue_limit: 1, custom_settings: { progressTarget: "divprogresscontainer", progressGroupTarget: "divprogressGroup", //progress object container_css: "progressobj", icoNormal_css: "IcoNormal", icoWaiting_css: "IcoWaiting", icoUpload_css: "IcoUpload", fname_css: "fle ftt", state_div_css: "statebarSmallDiv", state_bar_css: "statebar", percent_css: "ftt", href_delete_css: "ftt", //sum object /* 页面中不应出现以"cnt_"开头声明的元素 */ s_cnt_progress: "cnt_progress", s_cnt_span_text: "fle", s_cnt_progress_statebar: "cnt_progress_statebar", s_cnt_progress_percent: "cnt_progress_percent", s_cnt_progress_uploaded: "cnt_progress_uploaded", s_cnt_progress_size: "cnt_progress_size" }, debug: false, // Button settings button_image_url: "images/btnupload.png", button_width: "301", button_height: "171", button_placeholder_id: "spanButtonPlaceHolder", //button_text: \'<span class="theFont">上传文件</span>\', //button_text_style: ".theFont {background-image: url(\'images/btnupload.png\');width:301px;height:171px}", //button_text_left_padding: 12, //button_text_top_padding: 3, // The event handler functions are defined in handlers.js file_queued_handler: fileQueued, file_queue_error_handler: fileQueueError, upload_start_handler: uploadStart, upload_progress_handler: uploadProgress, upload_error_handler: uploadError, //uploadError upload_success_handler: uploadOk, //uploadSuccess //upload_complete_handler: uploadComplete, //uploadComplete file_dialog_complete_handler: fileDialogComplete }; swfu = new SWFUpload(settings); }; function uploadOk(file, serverData) { if (serverData != "0") { alert("上传完成!" + serverData); } else { alert("上传失败!"); swfu.setButtonDisabled(false); //启用上传按钮 } } </script> </head> <body> <div id="main_upload"> <form id="frmMain" action="upload.ashx" runat="server" enctype="multipart/form-data"> <span id="spanButtonPlaceHolder"></span> <div id="divprogresscontainer"></div> <div id="divprogressGroup"></div>
http://www.cnblogs.com/webapi/p/5811304.html </form> </div> </body> </html>
uploadFile.ashx代码
<%@ WebHandler Language="C#" Class="uploadFile" %> using System; using System.Web; using System.IO; public class uploadFile : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string PathName = HttpContext.Current.Server.MapPath("file/"); string NewsName = DateTime.Now.ToString("yyyyMMddHHmmssfff"); string NewsPathName = Path.Combine(PathName, NewsName); DateTime Dtime = System.DateTime.Now; HttpPostedFile file = HttpContext.Current.Request.Files["Filedata"]; if (file != null && file.ContentLength > 0 && !string.IsNullOrEmpty(file.FileName)) { file.SaveAs(NewsPathName + Path.GetFileName(file.FileName)); } context.Response.Write("ok123"); context.Response.End(); } public bool IsReusable { get { return false; } } }
源码打包下载地址:http://download.csdn.net/detail/pan524365501/9614045
本文地址:http://www.cnblogs.com/webapi/p/5811304.html
以上是关于asp文件上传:文件上传 源代码的主要内容,如果未能解决你的问题,请参考以下文章