public static bool DownloadFile(ProductFile file, HotcakesApplication app)
        {
            var    extension = Path.GetExtension(file.FileName);
            var    name      = Path.GetFileName(file.FileName);
            double fileSize  = 0;

            var storeId      = app.CurrentRequestContext.CurrentStore.Id;
            var diskFileName = file.Bvin + "_" + file.FileName + ".config";

            if (!DiskStorage.FileVaultFileExists(storeId, diskFileName))
            {
                return(false);
            }

            var bytes = DiskStorage.FileVaultGetBytes(storeId, diskFileName);

            var type = MimeTypes.FindTypeForExtension(extension);

            fileSize = bytes.Length;

            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearContent();
            HttpContext.Current.Response.ClearHeaders();

            if (!string.IsNullOrEmpty(type))
            {
                HttpContext.Current.Response.ContentType = type;
            }

            HttpContext.Current.Response.AppendHeader("content-disposition", "attachment; filename=" + name);

            if (fileSize > 0)
            {
                HttpContext.Current.Response.AddHeader("Content-Length", fileSize.ToString());
            }

            HttpContext.Current.Response.BinaryWrite(bytes);
            HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();

            return(true);
        }
예제 #2
0
        public ActionResult DownloadFile(string id)
        {
            var orderBvin = Request.QueryString["oid"] ?? string.Empty;
            var fileId    = id;
            var userId    = HccApp.CurrentCustomerId;

            var o    = HccApp.OrderServices.Orders.FindForCurrentStore(orderBvin);
            var file = HccApp.CatalogServices.ProductFiles.Find(fileId);

            // Validation checks
            if (file == null)
            {
                FlashWarning("The file could not be located for download.");
                return(View("DownloadFileError"));
            }
            if (o == null)
            {
                FlashWarning("The order number could not be located for downloads.");
                return(View("DownloadFileError"));
            }
            if (o.UserID != userId)
            {
                FlashWarning("This order does not belong to the current user. Please try again.");
                return(View("DownloadFileError"));
            }

            if (file.MaxDownloads <= 0)
            {
                file.MaxDownloads = 32000;
            }
            var currentCount = o.GetFileDownloadCount(file.Bvin);

            if (currentCount >= file.MaxDownloads)
            {
                FlashWarning(
                    "This file has already been downloaded the maximum number of allowed times. Please contact store administrator for assistance.");
                return(View("DownloadFileError"));
            }

            if (file.AvailableMinutes != 0)
            {
                if (DateTime.UtcNow.AddMinutes(file.AvailableMinutes * -1) > o.TimeOfOrderUtc)
                {
                    FlashWarning("File can no longer be downloaded. Its available time period has elapsed.");
                    return(View("DownloadFileError"));
                }
            }

            // Load File from Disk
            var extension = Path.GetExtension(file.FileName);
            var name      = Path.GetFileName(file.FileName);

            var storeId      = HccApp.CurrentStore.Id;
            var diskFileName = file.Bvin + "_" + file.FileName + ".config";

            if (!DiskStorage.FileVaultFileExists(storeId, diskFileName))
            {
                FlashWarning("The file source code not be located.");
                return(View("DownloadFileError"));
            }
            var bytes = DiskStorage.FileVaultGetBytes(storeId, diskFileName);
            var type  = MimeTypes.FindTypeForExtension(extension);

            // Record download
            o.IncreaseFileDownloadCount(file.Bvin);
            HccApp.OrderServices.Orders.Update(o);

            // Send File
            var r = new FileContentResult(bytes, type)
            {
                FileDownloadName = file.FileName
            };

            return(r);
        }