读书人

求上传下载源代码,该怎么处理

发布时间: 2012-01-08 22:48:50 作者: rapoo

求上传下载源代码
急需vs2005 c# 上传下载源代码。在一个系统中一个用户上传,另外的用户下载。

[解决办法]
.net下 文件下载相关函数

http://blog.csdn.net/hertcloud/archive/2007/03/22/1537371.aspx
[解决办法]
if (!System.IO.File.Exists(MapPath( "DownLoads/999.swf ")))
{
Response.Write( " <script language= 'javascript '> alert( '对不起,文件不存在! '); </script> ");
return;
}
Response.Clear();
Response.ClearHeaders();
Response.Charset = "GB2312 ";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = "application/octet-stream ";
FileInfo fi = new FileInfo(MapPath( "DownLoads/999.swf "));
Response.AddHeader( "Content-Disposition ", "attachment; filename= " + HttpUtility.UrlEncode(fi.Name));
Response.AddHeader( "Content-Length ", fi.Length.ToString());
byte[] tmpbyte = new byte[1024 * 8];
FileStream fs = fi.OpenRead();
int count;
while ((count = fs.Read(tmpbyte, 0, tmpbyte.Length)) > 0)
{
Response.BinaryWrite(tmpbyte);
Response.Flush();
}
fs.Close();
Response.End();
[解决办法]
上传文件的代码:
string filePath= " ",fileExtName= " ",mFileName,mPath;
string strContent=txtContent.Text;
string strID=txtID.Text;
strContent = CleanString.htmlInputText( strContent );
string strDate=DateTime.Now.ToString();
if( strContent == String.Empty )
{
Response.Write( " <script> ");
Response.Write( "alert( '请输入进程内容!!! '); ");
Response.Write( " </script> ");
return;
}
if( strContent.Length > 500 )
{
Response.Write( " <script> ");
Response.Write( "alert( '内容太长了..(500字以内)!!! '); ");
Response.Write( " </script> ");
return;
}
if( " "!=fileUp.PostedFile.FileName)
{
//取得文件路径
filePath=fileUp.PostedFile.FileName;
fileExtName=filePath.Substring(filePath.LastIndexOf( ". ")+1);
try
{
//取得与Web服务器上的指定虚拟路径相对应的物理文件路径
mPath=Server.MapPath( "~/UpFile/ ");
//取得文件名
mFileName=filePath.Substring(filePath.LastIndexOf( "\\ ")+1);
if( "doc "!=fileExtName&& "rar "!=fileExtName)
{
Response.Write( " <script> ");
Response.Write( "alert( '对不起,请选择doc或者rar格式的文件!!! '); ");
Response.Write( " </script> ");
}
else
{
string AllPath=mPath+mFileName;
fileUp.PostedFile.SaveAs(mPath+mFileName);
DBConn myDB = new DBConn();
string mySql = "insert into Message(MContent,CourseID,MDate,State,FilePath) values( ' " + strContent + " ', ' " + strID + " ', ' " + strDate + " ', '0 ', ' " + mFileName + " ') ";
myDB.Insert( mySql );
myDB.Close();
Response.Write( " <script> ");
Response.Write( "alert( '上传成功!!! '); ");
Response.Write( " </script> ");
}
}
catch(Exception ex)
{
Response.Redirect( "~/Error.aspx ");
}


下载文件的代码:
DBConn myDB = new DBConn();
string MID=Request.QueryString[ "id "].ToString().Trim();


string mySql = "select FilePath from Message where Message.MID= ' "+MID+ " ' ";
SqlDataReader mydr = myDB.getDataReader( mySql );
string filename= " ";
if( mydr.Read() )
{
filename=mydr[ "FilePath "].ToString();
}
string path = HttpContext.Current.Server.MapPath( "~/UpFile/ ");
Response.Clear();
Response.ContentType = "application/octet-stream ";
Response.AddHeader( "Content-Disposition ", "attachment;FileName= " + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));
Response.WriteFile(path+filename);
Response.End();


这些是文件上传和下载,自己看看吧

读书人网 >C#

热点推荐