书签 分享 收藏 举报 版权申诉 / 14

类型文件上传ASP.docx

  • 文档编号:6271271
  • 上传时间:2023-01-05
  • 格式:DOCX
  • 页数:14
  • 大小:19.76KB
 

 

 

 

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

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文件上传

///

///上传文件(自动分割)

///

///待上传的文件全路径名称(@"E:

\FTP\ftproot\20070228DQCK.zip")

///服务器的地址

///分割的字节大小

///主机用户ID

///当前字节指针

///成功返回"";失败则返回错误信息

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获取文件大小

///

///获取远程服务器文件字节大小

///

///待上传的文件全路径名称

///服务器的地址

///主机用户ID

///远程文件大小

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
提示  冰豆网所有资源均是用户自行上传分享,仅供网友学习交流,未经上传用户书面授权,请勿作他用。
关于本文
本文标题:文件上传ASP.docx
链接地址:https://www.bdocx.com/doc/6271271.html
关于我们 - 网站声明 - 网站地图 - 资源地图 - 友情链接 - 网站客服 - 联系我们

copyright@ 2008-2022 冰点文档网站版权所有

经营许可证编号:鄂ICP备2022015515号-1

收起
展开