私は以下のようなダウンロードファイルのハンドラを持っている:[許可=を読む]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using NiceFileExplorer.Classes;
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 binaryReader = new System.IO.BinaryReader(streamReader.BaseStream);
byte[] bytes = new byte[streamReader.BaseStream.Length];
binaryReader.Read(bytes, 0, (int)streamReader.BaseStream.Length);
if (bytes == null)
{
return;
}
streamReader.Close();
binaryReader.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 (HttpContext.Current.Session["User_ID"] != null)
{
WriteFile(bytes, fileName, filesize, MimeType + " " + extension_Without_dot, context.Response);
}
}
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(ext);
if (regKey != null && regKey.GetValue("Content Type") != null)
mimeType = regKey.GetValue("Content Type").ToString();
return mimeType;
}
public bool IsReusable
{
get
{
return false;
}
}
}
}
このハンドラの重要な部分は、WriteFile関数であり、それが完璧に動作します!
私は背後にある以下のようなコードからファイルをダウンロードするためにこのハンドラを呼び出す:私のウェブサイトの私のダウンロードリンクの
Response.Redirect("~/Handler.ashx?Downloadpath=" + HttpUtility.UrlEncode(DownloadPath));
1は、以下のようなものです:そう
http://localhost:5410/en/Download.aspx?Downloadpath=%2fFiles%2f%2fsamsung%2fGE2550_DEFAULT_MDL_V002.exe
、私は私のダウンロードをcontorlすることができますそのハンドラで簡単にリンクできます!
http://localhost:5410/Files/samsung/GE2550_DEFAULT_MDL_V002.exe
can download that file directly without that handler!
どのように私はこの直接ダウンロードを防ぐことができますいくつかの体はにそのリンクを変更したときに
私の問題はありますか?クエリ文字列にファイルへの実際の物理パスを入れ
こんにちは親愛なる仲間と答えに感謝!どのようにiisから見えないファイルを見ることができますか? – MoonLight
Filesフォルダのアクセス権を変更すると、エラーが発生します(Filesフォルダへのアクセスは拒否されました)。 – MoonLight
あなたは私にいくつかの方法についての説明をお願いします。 – MoonLight