PDA

View Full Version : نمایش لینک دانلود بعد از پرداخت



abolfazlahmadi
سه شنبه 29 اسفند 1396, 09:08 صبح
سلام
چه چور میشه بعداز پرداخت لینک دانلود نمایش داده بشه و لینک دانلود به صورت Key باشه

EnKamran
سه شنبه 29 اسفند 1396, 12:52 عصر
سلام دوست عزیز.
منطق به این صورت هست :
بعد از برگشت از بانک اعتبار سنجی هایی که لازم دارید رو انجام میدید(پرداخت شده؟کاربر عضو هست؟فاکتور متعلق به همین کاربر هست؟ و هر اعتبار سنجیی که لازم دارید.)
یک هندلر باید داشته باشید که آدرس فایل رو میگیره و اونجا میاد استریم مکنه فایل رو و برای کاربر شروع به دانلود میشه(با این کار کاربر دیگه لینک اصلی دانلود رو نمیبینه و فقط لینک هندلر رو میبینه برای دانلود که هربار بخواد دوباره دانلود بشه شما توی هندلر میای چک میکنی که آیای اجازه دانلود داره یا خیر)
یک نمونه کد از هندلر رو براتون میذارم شما میتونید شخصی سازیش کنید :

public class DownloadFile : IHttpHandler
{


public void ProcessRequest(HttpContext context)
{
var guid = new Guid(context.Request.QueryString["Download"]);
var oh = new OrderHelper();
var order = oh.GetOrderByGuid(guid);
if (order != null)
{
var ph = new ProductHelper();
var product = ph.GetProductById(order.ProductId);
if (product != null)
{
var url = product.FileAddress;
var fileName = url.Remove(0, url.LastIndexOf("/") + 1);


var expirationDate = order.ExpireDate;
var now = DateTime.Now;
if (expirationDate > now)
{
var pubHelper = new PublicHelper();
var title = "لینک دانلود " + product.ProductName;
var body = "برای دانلود روی لینک زیر کلیک کنید <br/> http://parsi-tech.com/DownloadFile.ashx?Download=" + order.Guid;
pubHelper.SendEmail(order.Email,title, body);


#region Download
//Create a stream for the file
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();
}
}
#endregion


}
else
{
oh.ChangeOrderStatus(order.PaymentAuthority, 5, order.PaymentVerificationStatus, order.PaymentVerificationRefId);
HttpContext.Current.Response.Write("زمان دانلود به پایان رسیده است.");
//زمان دانلود به پایان رسیده است
}
}
else
{
oh.ChangeOrderStatus(order.PaymentAuthority, 6, order.PaymentVerificationStatus, order.PaymentVerificationRefId);
HttpContext.Current.Response.Redirect("/404");
//فایل مورد نظر یافت نشد انتقال به صفحه 404
}
}
else
{
HttpContext.Current.Response.Redirect("/404");
//سفارش مورد نظر یافت نشد صفحه 404
}



}


public bool IsReusable
{
get
{
return false;
}
}
}

من دیگه کدها رو ویرایش نکردم، توش از متدهایی که خودم نوشتم استفاده کردم شما بسته به نیازت باید تغییرات بدی.