PDA

View Full Version : دانلود فایل از URL در ASP.NET Core



EnKamran
جمعه 04 خرداد 1397, 10:52 صبح
سلام دوستان.
عزیزان دانلود فایل از روی هسات فعلی رو که خوب بلدم مشکلی نیست.
سوال من اینه:
بنده یک سایت دارم و یک هاست دانلود، فایلهایی که برای دانلود قرار میدم روی هاست دانلود هست.
حالا نمیخوام که کاربر لینک اصلی فایل رو ببینه.
تو ASP.NET Core کسی میدونه باید چکار کرد؟
من این کد رو دارم کار میکنم درست کار میکنه ولی مشکل اینجاست که با این کد من قابلیت ریزام دیگه ندارم!
var url = "dl.mysite.com/file.zip";
var fileName = ips.GetFileNameFromUrl(url);

var ext = ips.GetFileExtension(fileName);

var mimType = ips.GetMimeType(ext);

var stream = await Client.GetStreamAsync(url);

return new FileStreamResult(stream, mimType)
{
FileDownloadName = fileName
};

EnKamran
جمعه 04 خرداد 1397, 11:23 صبح
من این کار رو در Web Forms انجام دادم مشکلی هم نداشته همین کار رو دقیقا تو ASP.NET Core میخوام انجام بدم.
کد کاری که انچام دادم تو WebForms :
Stream stream = null;

//This controls how many bytes to read at a time and send to the client
int bytesToRead = 10000;

// Buffer to read bytes in chunk size specified above
byte[] buffer = new Byte[bytesToRead];

// The number of bytes read
try
{
//Create a WebRequest to get the file
HttpWebRequest fileReq = (HttpWebRequest)HttpWebRequest.Create(url);

//Create a response for this request
HttpWebResponse fileResp = (HttpWebResponse)fileReq.GetResponse();

if (fileReq.ContentLength > 0)
fileResp.ContentLength = fileReq.ContentLength;

//Get the Stream returned from the response
stream = fileResp.GetResponseStream();

// prepare the response to the client. resp is the client Response
var resp = HttpContext.Current.Response;

//Indicate the type of data being sent
resp.ContentType = "application/octet-stream";

//Name the file
resp.AddHeader("Content-Disposition", "attachment; filename="" + fileName + """);
resp.AddHeader("Content-Length", fileResp.ContentLength.ToString());

int length;
do
{
// Verify that the client is connected.
if (resp.IsClientConnected)
{
// Read data into the buffer.
length = stream.Read(buffer, 0, bytesToRead);

// and write it out to the response's output stream
resp.OutputStream.Write(buffer, 0, length);

// Flush the data
resp.Flush();

//Clear the buffer
buffer = new Byte[bytesToRead];
}
else
{
// cancel the download if client has disconnected
length = -1;
}
} while (length > 0); //Repeat until no data is read
}
finally
{
if (stream != null)
{
//Close the input stream
stream.Close();
}
}

مهدی کرامتی
جمعه 04 خرداد 1397, 13:02 عصر
من کد مورد نیاز شما رو در ASP.NET MVC 5.2 دارم، فقط باید یک فرصت کنم تو ASP.NET MVC Core تستش کنم. اوکی بود براتون میگذارم.

EnKamran
جمعه 04 خرداد 1397, 13:58 عصر
بسیار ممنونم جناب کرامتی.
منتظر هستم قربان

anubis_ir
جمعه 04 خرداد 1397, 14:00 عصر
پشتیبانی توکار ASP.NET Core 2.0 از Range headers (https://www.dotnettips.info/post/2803/%d8%a7%d8%b2-%d8%b3%d8%b1%da%af%db%8c%d8%b1%db%8c-%d9%85%d8%ac%d8%af%d8%af-%d9%84%d8%ba%d9%88-%d8%af%d8%b1%d8%ae%d9%88%d8%a7%d8%b3%d8%aa-%d9%88-%d8%b3%d8%b9%db%8c-%d9%85%d8%ac%d8%af%d8%af-%d8%af%d8%b1%db%8c%d8%a7%d9%81%d8%aa-%d9%81%d8%a7%db%8c%d9%84%e2%80%8c%d9%87%d8%a7%db%8 c-%d8%ad%d8%ac%db%8c%d9%85-%d8%aa%d9%88%d8%b3%d8%b7-httpclient#comment-15185)

EnKamran
جمعه 04 خرداد 1397, 14:28 عصر
پشتیبانی توکار ASP.NET Core 2.0 از Range headers (https://www.dotnettips.info/post/2803/%d8%a7%d8%b2-%d8%b3%d8%b1%da%af%db%8c%d8%b1%db%8c-%d9%85%d8%ac%d8%af%d8%af-%d9%84%d8%ba%d9%88-%d8%af%d8%b1%d8%ae%d9%88%d8%a7%d8%b3%d8%aa-%d9%88-%d8%b3%d8%b9%db%8c-%d9%85%d8%ac%d8%af%d8%af-%d8%af%d8%b1%db%8c%d8%a7%d9%81%d8%aa-%d9%81%d8%a7%db%8c%d9%84%e2%80%8c%d9%87%d8%a7%db%8 c-%d8%ad%d8%ac%db%8c%d9%85-%d8%aa%d9%88%d8%b3%d8%b7-httpclient#comment-15185)

کد و توضیحی که دیدم مربوط به فایلهایی بودی که روی هاست خودت هاست شده باشن، بنده هاست دانلودم یک سرور دیگه س و باید از اونجا بخونم.
البته تا جایی که میدونم زیاد درست نیست مخصوصا برای فایلهای حجیم چون برای بافر یا استریم کردن باید اول روی مموری دانلود بشه بعد از اونجا رو سیستم کاربر دانلود بشه.
حالا منتظر راه حل جناب کرامتی هستم ببینم چطور میشه.

EnKamran
شنبه 05 خرداد 1397, 10:50 صبح
من کد مورد نیاز شما رو در ASP.NET MVC 5.2 دارم، فقط باید یک فرصت کنم تو ASP.NET MVC Core تستش کنم. اوکی بود براتون میگذارم.

جناب کرامتی عزیز، ببخشید یخورده عجله میکنم چون گیرم بخاطر این موضوع در حدی که راضی شده بودم با Web Forms یک ساب دامین درست کنم و این وظیفه رو اون انجام بده و نتیجه کار رو به سایت اصلی که DotNet Core هست برگردونه ولی اگر اینکار رو که فرمودید بتونم انجام بدم که بی نهایت عالی میشه.
ممنون از شما

EnKamran
یک شنبه 06 خرداد 1397, 08:00 صبح
دوستان لطفا کمک کنید هر کاری میکنم نمیشه. بدجور گیر کردم.

EnKamran
یک شنبه 13 خرداد 1397, 10:50 صبح
جناب کرامتی عزیز بنده همچنان منتظر هستم.
واقعا نیاز دارم به این کد.
سپاس

مهدی کرامتی
سه شنبه 15 خرداد 1397, 17:04 عصر
با عرض پوزش از تاخیر در پاسخ، اینجا را ببینید:
https://vikingerik.wordpress.com/2011/06/24/asp-net-mvc-resuming-actions

لینک پکیج فوق در Nuget هم موجوده:
https://www.nuget.org/packages/MVC.ResumingActionResults/

اگر هم خواستید به سورس اش دسترسی داشته باشید قبل از اینکه CodePlex برای همیشه بسته شود می توانید آرشیو Respository اش رو دانلود کنید:
https://codeplexarchive.blob.core.windows.net/archive/projects/MvcResumingActions/MvcResumingActions.zip

این هم یک مثال از نحوه استفاده اش:
public ActionResult DownloadAttachment(int? id)
{
if (id == null)
{
ViewBag.Message = "شناسه ضمیمه مشخص نشده است.";
return View("Error");
}

var attachment = db.ArticleAttachments.Find(id);
if (attachment == null)
{
ViewBag.Message = "هیچ ضمیمه ای با این شناسه وجود ندارد.";
return View("Error");
}

string filepath = Server.MapPath(attachment.ArticleAttachmentFileUrl );
return new ResumingFilePathResult(filepath, "application/octet-stream", attachment.ArticleAttachmentOriginalFilename);
}

mehrdad-jalili
پنج شنبه 25 بهمن 1397, 03:37 صبح
با عرض پوزش از تاخیر در پاسخ، اینجا را ببینید:
https://vikingerik.wordpress.com/2011/06/24/asp-net-mvc-resuming-actions

لینک پکیج فوق در Nuget هم موجوده:
https://www.nuget.org/packages/MVC.ResumingActionResults/

اگر هم خواستید به سورس اش دسترسی داشته باشید قبل از اینکه CodePlex برای همیشه بسته شود می توانید آرشیو Respository اش رو دانلود کنید:
https://codeplexarchive.blob.core.windows.net/archive/projects/MvcResumingActions/MvcResumingActions.zip

این هم یک مثال از نحوه استفاده اش:
public ActionResult DownloadAttachment(int? id)
{
if (id == null)
{
ViewBag.Message = "شناسه ضمیمه مشخص نشده است.";
return View("Error");
}

var attachment = db.ArticleAttachments.Find(id);
if (attachment == null)
{
ViewBag.Message = "هیچ ضمیمه ای با این شناسه وجود ندارد.";
return View("Error");
}

string filepath = Server.MapPath(attachment.ArticleAttachmentFileUrl );
return new ResumingFilePathResult(filepath, "application/octet-stream", attachment.ArticleAttachmentOriginalFilename);
}



سلام برای فایل های حجیم جواب میده؟؟

مهدی کرامتی
پنج شنبه 25 بهمن 1397, 09:36 صبح
بله. من تا چند گیگابایت اش رو تست کردم.

mehrdad-jalili
پنج شنبه 25 بهمن 1397, 17:59 عصر
ممنون از پاسختون. این کد مشکل داره و پارامتر سوم رو نمیشناسه!