ASP.NET下载方式rar和exe的具体代码

ASP.NET下载方式rar和exe的具体代码,第1张

写个下载事件,调用一个下载方法就可以了:

方法:

public bool ResponseFile(string _fullPath, string downname, int pack)

{

//_fullPath 下载路径

//downname 下载后保持的名称

//pack 每秒下载速度

HttpRequest _Request = HttpContext.Current.Request

HttpResponse _Response = HttpContext.Current.Response

try

{

FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)

BinaryReader br = new BinaryReader(myFile)

try

{

_Response.AddHeader("Accept-Ranges", "bytes")

_Response.Buffer = false

long fileLength = myFile.Length

long startBytes = 0

_Response.AddHeader("Content-Length", (fileLength - startBytes).ToString())

if (startBytes != 0)

{

_Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength))

}

_Response.AddHeader("Connection", "Keep-Alive")

_Response.ContentType = "application/octet-stream"

_Response.AddHeader("Content-Disposition", "attachmentfilename=" + HttpUtility.UrlEncode(downname, System.Text.Encoding.UTF8))

//int pack = 102400//100K bytes 进行拆包,每包大小

byte[] buff = new byte[pack]

var contentLength = br.Read(buff, 0, pack)

double d = 1000// 限速时每个包的时间

Stopwatch wa = new Stopwatch()

while (contentLength != 0)

{

if (_Response.IsClientConnected)

{

wa.Restart()

_Response.BinaryWrite(buff)

_Response.Flush()

contentLength = br.Read(buff, 0, pack)

wa.Stop()

if (wa.ElapsedMilliseconds <d) //如果实际带宽小于限制时间就不需要等待

{

Thread.Sleep((int)(d - wa.ElapsedMilliseconds))

}

}

else

{

break

}

}

return true

}

catch

{

return false

}

finally

{

br.Close()

myFile.Close()

}

}

catch

{

return false

}

}

场景描述

在B/S环境下 客户提出批量导出员工照片功能 具体为 选中一个部门或者单位 系统能够批量下载所选单元的照片 下载到用户客户端

解决思路

由于系统中员工的照片存储在服务器硬盘上 因此 应该有两种方式供用户选择 其一 写一个C/S客户端 利用客户端功能 实现客户端批量下载 *** 作 其二 在现有ASP NET环境下 将所需照片文件合并成一个文件下载到用户客户端 比较而言 两种思路的难度都不大 但是考虑到系统的统一性 最终决定采用方案二 将文件打包后下载

实现步骤

在用户 *** 作界面 由用户选择员工 系统根据所选人员 在服务器上创建用于存储所选文件的临时文件夹 将所选文件拷贝至临时文件夹 然后调用RAR程序 对临时文件夹进行压缩 然后输出到客户端 最后删除临时文件夹

部分关键代码

创建临时文件夹

string Folder = DateTime Now ToString( HHMMss )

string tempFolder = Path Combine(ImagesPath Folder)

Directory CreateDirectory(tempFolder)

var empList = rs ToList()

拷贝照片文件

foreach (var x in empList)

{

File Copy(ImagesPath + @ \ + x ID + jpg tempFolder + @ \ + x DeptName + + x Name + + x ID + jpg )

}

产生RAR文件 及文件输出

RARsave(tempFolder tempFolder Folder)

ResponseFile(tempFolder + @ \ + Folder + rar )

public void RARsave(string patch string rarPatch string rarName)

{

String the_rar

RegistryKey the_Reg

Object the_Obj

String the_Info

ProcessStartInfo the_StartInfo

Process the_Process

try

{

the_Reg = Registry ClassesRoot OpenSubKey(@ WinRAR )

the_Obj = the_Reg GetValue( )

the_rar = the_Obj ToString()

the_Reg Close()

the_rar = the_rar Substring( the_rar Length )

Directory CreateDirectory(patch)

//命令参数

//the_Info = a    + rarName +   + @ C:Test? txt //文件压缩

the_Info = a + rarName +   + patch +   r

the_StartInfo = new ProcessStartInfo()

the_StartInfo FileName = WinRar //the_rar

the_StartInfo Arguments = the_Info

the_StartInfo WindowStyle = ProcessWindowStyle Hidden

//打包文件存放目录

the_StartInfo WorkingDirectory = rarPatch

the_Process = new Process()

the_Process StartInfo = the_StartInfo

the_Process Start()

the_Process WaitForExit()

the_Process Close()

}

catch (Exception ex)

{

throw ex

}

}

protected void ResponseFile(string fileName)

{

FileInfo fileInfo = new FileInfo(fileName)

Response Clear()

Response ClearContent()

Response ClearHeaders()

Response AddHeader( Content Disposition attachmentfilename= + fileName)

Response AddHeader( Content Length fileInfo Length ToString())

Response AddHeader( Content Transfer Encoding binary )

Response ContentType = application/octet stream

Response ContentEncoding = System Text Encoding GetEncoding( gb )

Response WriteFile(fileInfo FullName)

Response Flush()

string tempPath = fileName Substring( fileName LastIndexOf( \\ ))

DelDir(tempPath)

Directory Delete(tempPath)

Response End()

}

lishixinzhi/Article/program/net/201311/13948

微软官网就有http://download.microsoft.com/download/6/0/f/60fc5854-3cb8-4892-b6db-bd4f42510f28/dotnetfx35.exe不知道你说的Asp.net3.5是指你要运行,还是要开发,如果运行的话,上面这个是Framework3.5中文版,安装它就可以了.如果要开发的话,下载Vs2008就可以http://www.microsoft.com/downloads/details.aspx?familyid=83C3A1EC-ED72-4A79-8961-25635DB0192B&displaylang=zh-cn 这个是Vs2008专业版


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/yw/11181801.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-14
下一篇 2023-05-14

发表评论

登录后才能评论

评论列表(0条)

保存