文件上传ASP.docx
- 文档编号:6271271
- 上传时间:2023-01-05
- 格式:DOCX
- 页数:14
- 大小:19.76KB
文件上传ASP.docx
《文件上传ASP.docx》由会员分享,可在线阅读,更多相关《文件上传ASP.docx(14页珍藏版)》请在冰豆网上搜索。
文件上传ASP
<%@ Page language="c#" Src="UpLoad.aspx.cs" AutoEventWireup="false" Inherits="WebPortal.Upload" %>
DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
-----------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;
namespace WebPortal
{
public class Upload :
System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button UploadButton;
protected System.Web.UI.WebControls.Label strStatus;
private void Page_Load(object sender, System.EventArgs e)
{
Session["uptime"]=DateTime.Now.Date.ToShortDateString();
Session["uptime2"]=DateTime.Now.ToString().Replace("下午","").Replace("上午","").Replace("/","").Replace("-","").Replace(":
","").Replace(" ","");
if (this.IsPostBack) this.SaveImages();
}
private Boolean SaveImages()
{
HttpFileCollection files = HttpContext.Current.Request.Files;
System.Text.StringBuilder strMsg = new System.Text.StringBuilder();
strMsg.Append("上傳的文件分別是:
");
try
{
for(int iFile = 0; iFile < files.Count; iFile++)
{
///'?
查文件?
展名字
HttpPostedFile postedFile = files[iFile];
string fileName, fileExtension,fileSize;
fileName = System.IO.Path.GetFileName(postedFile.FileName);
if((postedFile.ContentLength)>10485760)
{
Response.Write("");
}
else
{
if (fileName !
= "")
{
fileExtension = System.IO.Path.GetExtension(fileName);
strMsg.Append("上傳的文件類型:
" + postedFile.ContentType.ToString() + "
");
strMsg.Append("客戶端文件地址:
" + postedFile.FileName + "
");
strMsg.Append("上傳文件的文件名:
" + fileName + "
");
strMsg.Append("上傳文件的擴展名:
" + fileExtension + "
");
strMsg.Append("上傳文件的路?
:
" + Request.PhysicalApplicationPath + "uploadfile"+"
");
strMsg.Append("上?
文件的路?
:
" + Server.MapPath(fileName) + "
");
strMsg.Append("上傳文件的路徑:
" + "uploadfile/"+ fileName +"
");
strMsg.Append("上傳文件的大小:
" + countSize(postedFile.ContentLength) + "
");
///'可根据?
展名字的不同保存到不同的文件?
///注意:
可能要修改你的文件?
的匿名?
入?
限。
postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("../uploadfile/") + fileName);
//postedFile.SaveAs(System.Web.HttpContext.Current.Request.MapPath("../uploadfile/") + Session["uptime2"]+".xls");
string nowDSN=ConfigurationSettings.AppSettings["data"];
SqlConnection myConnection=new SqlConnection(nowDSN);
string CommandText;
SqlCommand myCommand = new SqlCommand();
myCommand.Connection=myConnection;
myCommand.Connection.Open();
string filename3=Session["uptime2"]+".xls";
myCommand.CommandText= "insert into uploadfile(filename,filesize,filemappath,fileExtension,uptime,filename2)values('"+fileName+"','"+countSize(postedFile.ContentLength)+"','"+Request.PhysicalApplicationPath+"uploadfile\\"+fileName+"','"+fileExtension+"','"+Session["uptime"]+"','"+filename3+"')";
myCommand.ExecuteNonQuery();
}
else
{
Response.Write("");
}
}
}
strStatus.Text = strMsg.ToString();
return true;
}
catch(System.Exception Ex)
{
strStatus.Text = Ex.Message;
return false;
}
}
#region Web 窗体?
?
器生成的代?
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.ID = "Upload";
this.Load += new System.EventHandler(this.Page_Load);
}
public string countSize(long size) //?
是?
算文件大小的方法
{
string strSize="";
long lnSize=0;
lnSize=size;
if(lnSize<1024)
strSize=lnSize.ToString()+"byte";
if(lnSize>=1024 && lnSize<1048576)
strSize=(lnSize/1024).ToString() + "K";
if(lnSize>=1048576 && lnSize<=10485760)
strSize=(lnSize/1024/1024).ToString() + "M";
return strSize;
}
#endregion
}
}
哦,差点看错了,是C#编程的呀
可以看看这个.
用C#写的文件断点续传、下载的类
本用例是采用C#上传、Servlet进行接收
C#作为客户端、Servlet作为服务器进行文件断点下载
有调试不通过的,给我Mail:
lijiangchxp@
usingSystem;
usinglog4net;
usingSystem.Collections;
usingSystem.Text;
usingSystem.IO;
usingSystem.Net;
usinglog4net.Config;
usingChxp.Business;
namespaceChxp.Service
{
publicclassFileLib
{
#region属性
privatestringfileName="";
publicstringFileName
{
get{returnfileName;}
set{fileName=value;}
}
#endregion
privatestaticreadonlyILogLOG=LogManager.GetLogger(typeof(FileLib));
#region文件上传
///
///上传文件(自动分割)
///
///
\FTP\ftproot\20070228DQCK.zip")
///
///
///
///
///
publicstringUpLoadFile(stringfilePath,stringhostURL,intbyteCount,stringuserID,longcruuent)
{
stringtmpURL=hostURL;
byteCount=byteCount*1024;
//http:
//localhost:
8080/fism/app?
service=fileupload&beanId=com.cfcc.fism.service.upload.CollFileSaveServiceImpl&action=upload&filename=AI1215900000020051130411.zip&userid=test&npos=333
//action=length
System.Net.WebClientWebClientObj=newSystem.Net.WebClient();
FileStreamfStream=newFileStream(filePath,FileMode.Open,FileAccess.Read);
BinaryReaderbReader=newBinaryReader(fStream);
longlength=fStream.Length;
stringsMsg="版式上传成功";
stringfileName=filePath.Substring(filePath.LastIndexOf('\\')+1);
try
{
#region续传处理
byte[]data;
if(cruuent>0)
{
fStream.Seek(cruuent,SeekOrigin.Current);
}
#endregion
#region分割文件上传
for(;cruuent<=length;cruuent=cruuent+byteCount)
{
if(cruuent+byteCount>length)
{
data=newbyte[Convert.ToInt64((length-cruuent))];
bReader.Read(data,0,Convert.ToInt32((length-cruuent)));
}
else
{
data=newbyte[byteCount];
bReader.Read(data,0,byteCount);
}
try
{
LOG.Debug(data);
//***
hostURL=tmpURL+"&action=upload"+"&filename="+fileName+"&userid="+userID+"&npos="+cruuent.ToString();
byte[]byRemoteInfo=WebClientObj.UploadData(hostURL,"POST",data);
stringsRemoteInfo=System.Text.Encoding.Default.GetString(byRemoteInfo);
//获取返回信息
if(sRemoteInfo.Trim()!
="")
{
sMsg=sRemoteInfo;
break;
}
}
catch(Exceptionex)
{
sMsg=ex.ToString();
break;
}
#endregion
}
}
catch(Exceptionex)
{
sMsg=sMsg+ex.ToString();
}
try
{
bReader.Close();
fStream.Close();
}
catch(ExceptionexMsg)
{
sMsg=exMsg.ToString();
}
GC.Collect();
returnsMsg;
}
#endregion
#region获取文件大小
///
///获取远程服务器文件字节大小
///
///
///
///
///
publiclongGetRemoteFileLength(stringfilePath,stringhostURL,stringuserID)
{
longlength=0;
System.Net.WebClientWebClientObj=newSystem.Net.WebClient();
stringfileName=filePath.Substring(filePath.LastIndexOf('\\')+1);
hostURL=hostURL+"&action=length"+"&filename="+fileName+"&userid="+userID+"&npos=0";
byte[]data=newbyte[0];
byte[]byRemoteInfo=WebClientObj.UploadData(hostURL,"POST",data);
stringsRemoteInfo=System.Text.Encoding.Default.GetString(byRemoteInfo);//主系统没有作异常处理
try
{
length=Convert.ToInt64(sRemoteInfo);
}
catch(Exceptionexx)
{
LOG.Error("FileLib类GetRemoteFileLength()中length=Convert.ToInt64(sRemoteInfo)语句异常:
"+exx.Message);//我们强制处理异常
length=0;
}
GC.Collect();
returnlength;
}
///
///获得本地文件字节大小
///
///
///
publiclongGetLocalFileLength(stringfilePath)
{
longlength=0;
try
{
stringfileName=filePath.Substring(filePath.LastIndexOf('\\')+1);
FileStreams=newFileStream(filePath,FileMode.Open);
length=s.Length;
s.Close();
}
catch(Exceptionex)
{
LOG.Error("FileLib类中获取本地文件大小异常:
"+ex.Message);
}
returnlength;
}
#endregion
#region文件下载
publicboolDownLoadFile(stringlocalPath,stringhostURL,intbyteCount,stringuserID,longcruuent)
{
boolresult=true;
stringtmpURL=hostURL;
byteCount=byteCount*1024;
hostURL=tmpURL+"&npos="+cruuent.ToString();
System.IO.FileStr
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 文件 上传 ASP
![提示](https://static.bdocx.com/images/bang_tan.gif)