楼上说的有道理.文件打包好后,有些情况下可能下载不了.那么可以试着让IE调用,那么IE可以下载所有类型的文件. public void DownLoading(string filePath) { System.IO.Stream iStream = null; // Buffer to read 10K bytes in chunk: byte[] buffer = new Byte[10240]; // Length of the file: int length; // Total bytes to read: long dataToRead; // Identify the file to download including its path. string filepath = filePath; // Identify the file name. string filename = System.IO.Path.GetFileName(filepath); try { // Open the file.并且允许多线程读取。 iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read,System.IO.FileShare.Read); Response.Clear(); // Total bytes to read: dataToRead = iStream.Length; long p = 0; if(Request.Headers["Range"]!=null) { // Web端得响应头如下:Range:bytes=1024- 所以我们要去掉无用信息。得到中间的大小信息。如1024。 Response.StatusCode = 206; p = long.Parse( Request.Headers["Range"].Replace("bytes=","").Replace("-","")); } if(p != 0) { Response.AddHeader("Content-Range","bytes " + p.ToString() + "-" + ((long)(dataToRead - 1)).ToString() + "/" + dataToRead.ToString()); } Response.AddHeader("Content-Length",((long)(dataToRead-p)).ToString()); Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(Request.ContentEncoding.GetBytes(filename))); iStream.Position = p; dataToRead = dataToRead - p; // Read the bytes. while (dataToRead > 0) { // Verify that the client is connected. if (Response.IsClientConnected) { // Read the data in buffer. length = iStream.Read(buffer, 0, 10240); // Write the data to the current output stream. Response.OutputStream.Write(buffer, 0, length); // Flush the data to the HTML output. Response.Flush(); buffer= new Byte[10240]; dataToRead = dataToRead - length; } else { //prevent infinite loop if user disconnects dataToRead = -1; } } } catch (Exception ex) { // Trap the error, if any. Response.Write("Error : " + ex.Message); } finally { if (iStream != null) { //Close the file. iStream.Close(); } Response.End(); } }
http://community.csdn.net/Expert/topic/5118/5118686.xml?temp=6.414431E-02
看看上面的帖的回复
2.根据图片ID得到图片物理路径
3.用比如SharpZipLib之类的类库压缩打包这些图片
4.向客户端输出压缩文件
public void DownLoading(string filePath)
{
System.IO.Stream iStream = null;
// Buffer to read 10K bytes in chunk:
byte[] buffer = new Byte[10240]; // Length of the file:
int length; // Total bytes to read:
long dataToRead; // Identify the file to download including its path.
string filepath = filePath; // Identify the file name.
string filename = System.IO.Path.GetFileName(filepath); try
{
// Open the file.并且允许多线程读取。
iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
System.IO.FileAccess.Read,System.IO.FileShare.Read);
Response.Clear(); // Total bytes to read:
dataToRead = iStream.Length; long p = 0;
if(Request.Headers["Range"]!=null)
{
// Web端得响应头如下:Range:bytes=1024- 所以我们要去掉无用信息。得到中间的大小信息。如1024。
Response.StatusCode = 206;
p = long.Parse( Request.Headers["Range"].Replace("bytes=","").Replace("-",""));
}
if(p != 0)
{
Response.AddHeader("Content-Range","bytes " + p.ToString() + "-" + ((long)(dataToRead - 1)).ToString() + "/" + dataToRead.ToString());
}
Response.AddHeader("Content-Length",((long)(dataToRead-p)).ToString());
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + System.Web.HttpUtility.UrlEncode(Request.ContentEncoding.GetBytes(filename))); iStream.Position = p;
dataToRead = dataToRead - p;
// Read the bytes.
while (dataToRead > 0)
{
// Verify that the client is connected.
if (Response.IsClientConnected)
{
// Read the data in buffer.
length = iStream.Read(buffer, 0, 10240); // Write the data to the current output stream.
Response.OutputStream.Write(buffer, 0, length); // Flush the data to the HTML output.
Response.Flush(); buffer= new Byte[10240];
dataToRead = dataToRead - length;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}
}
catch (Exception ex)
{
// Trap the error, if any.
Response.Write("Error : " + ex.Message);
}
finally
{
if (iStream != null)
{
//Close the file.
iStream.Close();
}
Response.End();
}
}
然后,后台新建进程调用winrar来压缩那个文件夹。
此时,可以用iis新建一个下载专用的虚拟目录。
把那新压缩的文件转移到那个目录里,或者你的虚拟目录本来就指向当前的文件夹。