majid_darab
دوشنبه 09 آبان 1390, 03:50 صبح
با سلام و عرض خسته نباشید خدمت دوستان عزیز :
من از handler زیر برای دانلود فایل استفاده می کنم.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
namespace NiceFileExplorer
{
/// <summary>
/// Summary description for HandlerForMyFE
/// </summary>
public class HandlerForMyFE : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
private HttpContext _context;
private HttpContext Context
{
get
{
return _context;
}
set
{
_context = value;
}
}
public void ProcessRequest(HttpContext context)
{
Context = context;
string filePath = context.Request.QueryString["Downloadpath"];
filePath = context.Server.MapPath(filePath);
if (filePath == null)
{
return;
}
System.IO.StreamReader streamReader = new System.IO.StreamReader(filePath);
System.IO.BinaryReader br = new System.IO.BinaryReader(streamReader.BaseStream);
byte[] bytes = new byte[streamReader.BaseStream.Length];
br.Read(bytes, 0, (int)streamReader.BaseStream.Length);
if (bytes == null)
{
return;
}
streamReader.Close();
br.Close();
string fileName = System.IO.Path.GetFileName(filePath);
string MimeType = GetMimeType(fileName);
string extension = System.IO.Path.GetExtension(filePath);
char[] extension_ar = extension.ToCharArray();
string extension_Without_dot = string.Empty;
for (int i = 1; i < extension_ar.Length; i++)
{
extension_Without_dot += extension_ar[i];
}
string filesize = string.Empty;
FileInfo f = new FileInfo(filePath);
filesize = f.Length.ToString();
//if (extension == ".jpg")
//{ // Handle *.jpg and
// WriteFile(bytes, fileName, "image/jpeg jpeg jpg jpe", context.Response);
//}
//else if (extension == ".gif")
//{// Handle *.gif
// WriteFile(bytes, fileName, "image/gif gif", context.Response);
//}
WriteFile(bytes, fileName, filesize, MimeType + " " + extension_Without_dot, context.Response);
////context = HttpContext
//context.Response.Clear();
//context.Response.ContentType = MimeType;
//context.Response.TransmitFile(filePath);
//context.Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
//FileInfo f = new FileInfo(filePath);
//context.Response.AddHeader("Content-Length", f.Length.ToString());
//context.Response.Flush();
//context.Response.End();
}
private void WriteFile(byte[] content, string fileName, string filesize, string contentType, HttpResponse response)
{
response.Buffer = true;
response.Clear();
response.ContentType = contentType;
response.AddHeader("content-disposition", "attachment; filename=" + fileName);
response.AddHeader("Content-Length", filesize);
response.BinaryWrite(content);
response.Flush();
response.End();
}
private string GetMimeType(string fileName)
{
string mimeType = "application/unknown";
string ext = System.IO.Path.GetExtension(fileName).ToLower();
Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ex t);
if (regKey != null && regKey.GetValue("Content Type") != null)
mimeType = regKey.GetValue("Content Type").ToString();
return mimeType;
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
نحوه کار این handler این است که از query string با نام path آدرس فایل مورد نظرو دریافت و سپس اونو برای دانلود آماده می کند و پنجره دانلود در بروزر ظاهر می گردد.
زمانی که با دانلود منیجر اقدام به دانلود فایل می کنیم می بینیم که فایل مورد نظر قابل resume نیست!
چطور می شود دانلود فایل مورد نظر را resume able کرد؟
با تشکر از حسن توجه شما
من از handler زیر برای دانلود فایل استفاده می کنم.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
namespace NiceFileExplorer
{
/// <summary>
/// Summary description for HandlerForMyFE
/// </summary>
public class HandlerForMyFE : IHttpHandler, System.Web.SessionState.IRequiresSessionState
{
private HttpContext _context;
private HttpContext Context
{
get
{
return _context;
}
set
{
_context = value;
}
}
public void ProcessRequest(HttpContext context)
{
Context = context;
string filePath = context.Request.QueryString["Downloadpath"];
filePath = context.Server.MapPath(filePath);
if (filePath == null)
{
return;
}
System.IO.StreamReader streamReader = new System.IO.StreamReader(filePath);
System.IO.BinaryReader br = new System.IO.BinaryReader(streamReader.BaseStream);
byte[] bytes = new byte[streamReader.BaseStream.Length];
br.Read(bytes, 0, (int)streamReader.BaseStream.Length);
if (bytes == null)
{
return;
}
streamReader.Close();
br.Close();
string fileName = System.IO.Path.GetFileName(filePath);
string MimeType = GetMimeType(fileName);
string extension = System.IO.Path.GetExtension(filePath);
char[] extension_ar = extension.ToCharArray();
string extension_Without_dot = string.Empty;
for (int i = 1; i < extension_ar.Length; i++)
{
extension_Without_dot += extension_ar[i];
}
string filesize = string.Empty;
FileInfo f = new FileInfo(filePath);
filesize = f.Length.ToString();
//if (extension == ".jpg")
//{ // Handle *.jpg and
// WriteFile(bytes, fileName, "image/jpeg jpeg jpg jpe", context.Response);
//}
//else if (extension == ".gif")
//{// Handle *.gif
// WriteFile(bytes, fileName, "image/gif gif", context.Response);
//}
WriteFile(bytes, fileName, filesize, MimeType + " " + extension_Without_dot, context.Response);
////context = HttpContext
//context.Response.Clear();
//context.Response.ContentType = MimeType;
//context.Response.TransmitFile(filePath);
//context.Response.AddHeader("content-disposition", "attachment;filename=" + fileName);
//FileInfo f = new FileInfo(filePath);
//context.Response.AddHeader("Content-Length", f.Length.ToString());
//context.Response.Flush();
//context.Response.End();
}
private void WriteFile(byte[] content, string fileName, string filesize, string contentType, HttpResponse response)
{
response.Buffer = true;
response.Clear();
response.ContentType = contentType;
response.AddHeader("content-disposition", "attachment; filename=" + fileName);
response.AddHeader("Content-Length", filesize);
response.BinaryWrite(content);
response.Flush();
response.End();
}
private string GetMimeType(string fileName)
{
string mimeType = "application/unknown";
string ext = System.IO.Path.GetExtension(fileName).ToLower();
Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ex t);
if (regKey != null && regKey.GetValue("Content Type") != null)
mimeType = regKey.GetValue("Content Type").ToString();
return mimeType;
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
نحوه کار این handler این است که از query string با نام path آدرس فایل مورد نظرو دریافت و سپس اونو برای دانلود آماده می کند و پنجره دانلود در بروزر ظاهر می گردد.
زمانی که با دانلود منیجر اقدام به دانلود فایل می کنیم می بینیم که فایل مورد نظر قابل resume نیست!
چطور می شود دانلود فایل مورد نظر را resume able کرد؟
با تشکر از حسن توجه شما