Saturday, May 20, 2017

Download a file

private void DownloadUserPdf(string userId)
        {
            string path = HttpContext.Server.MapPath(@"~\UserPDF\userPdf.pdf");
            WebClient webClient = new WebClient();

            String dPath = String.Empty;
            RegistryKey rKey = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Internet Explorer\Main");
            if (rKey != null)
                dPath = (String)rKey.GetValue("Default Download Directory");
            if (String.IsNullOrEmpty(dPath))
                dPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile) + "\\downloads";

            string filePath = dPath + @"\userFileAgain.pdf";

            webClient.DownloadFile(new Uri(""), filePath);

            Converts the PdfDocument object to byte form.
            byte[] docBytes = stream.ToArray();
            Loads the byte array in PdfLoadedDocument

            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.AppSettings["StorageConnectionString"]); //connection string is copied from Azure storage account's Settings
            CloudBlobClient client = storageAccount.CreateCloudBlobClient();
            CloudBlobContainer myContainer = client.GetContainerReference("assetmarkbat");
            var permissions = myContainer.GetPermissions();
            permissions.PublicAccess = BlobContainerPublicAccessType.Blob;
            myContainer.SetPermissions(permissions);

            CloudBlockBlob blockBlob = myContainer.GetBlockBlobReference(userId + ".pdf");
            blockBlob.Properties.ContentType = "application/pdf";
            blockBlob.UploadFromStream(stream);
            blockBlob.UploadFromByteArray(docBytes, 0, docBytes.Count());
            Save blob contents to a file.
            using (var fileStream = System.IO.File.OpenWrite(@"C:\" + userId + ".pdf"))
            {
                blockBlob.DownloadToStream(fileStream);
            }


        }

No comments:

Post a Comment