PDA

View Full Version : سوال: دانلود فایل از سرور دیگر



Mohammad_dn
پنج شنبه 22 آبان 1393, 14:18 عصر
با سلام
من با استفاده از کد زیر یک فایل رو برای دانلود میزاررم


public bool DownloadFile(HttpContext httpContext, string filePath, long speed)
{
bool ret = true;
try
{
switch (Request.HttpMethod.ToString().ToUpper())
{ //support Get and head method
case "GET":
case "HEAD":
case "POST":
break;
default:
Response.StatusCode = 501;
return false;
}
if (!System.IO.File.Exists(filePath))
{
Response.StatusCode = 404;
return false;
}



long startBytes = 0;
int packSize = 1024 * 10; //read in block,every block 10K bytes
string fileName = Path.GetFileName(filePath);
FileStream myFile = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(myFile);
long fileLength = myFile.Length;

int sleep = (int)Math.Ceiling(1000.0 * packSize / speed);//the number of millisecond
string lastUpdateTiemStr = System.IO.File.GetLastWriteTimeUtc(filePath).ToStr ing("r");
string eTag = HttpUtility.UrlEncode(fileName, Encoding.UTF8) + lastUpdateTiemStr;

//validate whether the file is too large
if (myFile.Length > Int32.MaxValue)
{
Response.StatusCode = 413;
return false;
}

if (Request.Headers["If-Range"] != null)
{

if (Request.Headers["If-Range"].Replace("\"", "") != eTag)
{
Response.StatusCode = 412;
return false;
}
}

try
{

Response.Clear();
Response.Buffer = false;
//Response.AddHeader("Content-MD5", GetMD5Hash(myFile));
Response.AddHeader("Accept-Ranges", "bytes");
Response.AppendHeader("ETag", "\"" + eTag + "\"");
Response.AppendHeader("Last-Modified", lastUpdateTiemStr);
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment;filename=" +

HttpUtility.UrlEncode(fileName, Encoding.UTF8).Replace("+", "%20"));
Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
Response.AddHeader("Connection", "Keep-Alive");
Response.ContentEncoding = Encoding.UTF8;
if (Request.Headers["Range"] != null)
{
Response.StatusCode = 206;
string[] range = Request.Headers["Range"].Split(new char[] { '=', '-' });
startBytes = Convert.ToInt64(range[1]);
if (startBytes < 0 || startBytes >= fileLength)
{
return false;
}
}
if (startBytes > 0)
{
Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength));
}


//send data
br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
int maxCount = (int)Math.Ceiling((fileLength - startBytes + 0.0) / packSize);//download in block
for (int i = 0; i < maxCount && Response.IsClientConnected; i++)
{
Response.BinaryWrite(br.ReadBytes(packSize));
Response.Flush();
if (sleep > 1) Thread.Sleep(sleep);
}

}
catch
{
ret = false;
}
finally
{
br.Close();
myFile.Close();
}
}
catch
{
ret = false;
}
return ret;
}




این کد فقط زمانی کار میکنه که فایل در سرور خودمون باشه..در واقع توی همون سایت باشه...اما من میخوام بتونم با دستوری مشابه از یه سرور دیگه فایل برای دانلود بزارم...
مثلا آدرس:http://dl2.download-music.in/sad/Aidin%20Bahri%20Nezhad%20%E2%80%93%20Sokoote%20Mot lagh320.mp3

چطور میشه اینکارو انجام داد؟