نیازمند یک کلاس کامل برای انجام عملیات برروی پایگاه داده

ساخت وبلاگ
نقل قول نوشته اصلی توسط number نمایش پست ها

سلام. به نظر شما فایلهای خودمون رو روی هاست ASP.NET داخل دیتابیس ذخیره کنیم بهتره یا داخل یک فولدر بریزیم و به کاربرا لینک بدیم؟

اگر تعداد و حجم کل فایلهاتون زیاد نیست می تونین فایلها رو به Byte تبدیل کنین و بعد از کد گذاری (برای جلوگیری از دزدی احتمالی) داخل پایگاه داده ذخیره بفرمائین. ولی اگر حجم فایلهای شما خیلی زیاده بهتره که داخل یکی از فولدرهای محافظت شده (مانند App_Data) ذخیره بفرمائین و بعد لینک دانلود غیر مستقیم بدین. با استفاده از کلاس زیر می تونین این کار رو انجام بدین:

کد:

/// <summary>/// Download Large Files! For example more than 100MB!/// </summary>public void Download(int id)
{ // ************************************************** string strFileName = string.Format("{0}.zip", id); string strRootRelativePathName = string.Format("~/App_Data/Files/{0}", strFileName); string strPathName = Server.MapPath(strRootRelativePathName); if (System.IO.File.Exists(strPathName) == false) { retu; } // ************************************************** System.IO.Stream oStream = null; try { // Open the file oStream = new System.IO.FileStream (path: strPathName, mode: System.IO.FileMode.Open, share: System.IO.FileShare.Read, access: System.IO.FileAccess.Read); // ************************************************** Response.Buffer = false; // Setting the unknown [ContentType] // will display the saving dialog for the user Response.ContentType = "application/octet-stream"; // With setting the file name, // in the saving dialog, user will see // the [strFileName] name instead of [download]! Response.AddHeader("Content-Disposition", "attachment; filename=" + strFileName); long lngFileLength = oStream.Length; // Notify user (client) the total file length Response.AddHeader("Content-Length", lngFileLength.ToString()); // ************************************************** // Total bytes that should be read long lngDataToRead = lngFileLength; // Read the bytes of file while (lngDataToRead > 0) { // The below code is just for testing! So we commented it! //System.Threading.Thread.Sleep(200); // Verify that the client is connected or not? if (Response.IsClientConnected) { // 8KB int intBufferSize = 8 * 1024; // Create buffer for reading [intBufferSize] bytes from file byte[] bytBuffers = new System.Byte[intBufferSize]; // Read the data and put it in the buffer. int intTheBytesThatReallyHasBeenReadFromTheStream = oStream.Read(buffer: bytBuffers, offset: 0, count: intBufferSize); // Write the data from buffer to the current output stream. Response.OutputStream.Write (buffer: bytBuffers, offset: 0, count: intTheBytesThatReallyHasBeenReadFromTheStream); // Flush (Send) the data to output // (Don't buffer in server's RAM!) Response.Flush(); lngDataToRead = lngDataToRead - intTheBytesThatReallyHasBeenReadFromTheStream; } else { // Prevent infinite loop if user disconnected! lngDataToRead = -1; } } } catch { } finally { if (oStream != null) { //Close the file. oStream.Close(); oStream.Dispose(); oStream = null; } Response.Close(); }
}

سافتو...
ما را در سایت سافتو دنبال می کنید

برچسب : نویسنده : کاوه محمدزادگان softo بازدید : 178 تاريخ : پنجشنبه 5 فروردين 1395 ساعت: 0:30