Let me start off by saying I'm sure this is something that's quite simple, unfortunately I just can't seem to figure it out. So here's my problem, I query the database, return what I need, zip it all up, and prompt user to save. When You attempt to open this, there are no files inside the folder. Where has my data gone? I stepped through everything, and it appears to write everything properly. Any help would be greatly appreciated!!
if (e.CommandName == "DownloadAttachment")
{
e.Canceled = true;
// Create a zip and send it to the client.
//Response.Write(#"<script language='javascript'>alert('Details saved successfully')</script>");
var item = e.Item as GridEditableItem;
fileId = (int)item.GetDataKeyValue("Unique");
FileData[] allrecords = null;
using (
SqlConnection conn =
new SqlConnection(ConfigurationManager.ConnectionStrings["PtcDbModelEntities"].ConnectionString))
{
using (
SqlCommand comm = new SqlCommand("Select Unique1, BinaryData, FileName from PtcDbTracker.dbo.CafFileTable where Unique1=#fileId AND FileName IS NOT NULL", conn))
{
comm.Parameters.Add(new SqlParameter("#fileId", fileId));
conn.Open();
using (var reader = comm.ExecuteReader())
{
var list = new List<FileData>();
while (reader.Read())
{
list.Add(new FileData { Unique1 = reader.GetInt32(0) });
long len = reader.GetBytes(1, 0, null, 0, 0);
Byte[] buffer = new byte[len];
list.Add(new FileData { BinaryData = (byte)reader.GetBytes(1, 0, buffer, 0, (int)len), FileName = reader.GetString(2) });
allrecords = list.ToArray();
}
}
conn.Close();
}
}
using (var compressedFileStream = new MemoryStream())
{
//Create an archive and store the stream in memory.
using (var zipArchive = new ZipArchive(compressedFileStream, ZipArchiveMode.Update, false))
{
if (allrecords != null)
{
foreach (var record in allrecords)
{
//Create a zip entry for each attachment
if (record.FileName != null)
{
var zipEntry = zipArchive.CreateEntry(record.FileName);
//Get the stream of the attachment
using (var originalFileStream = new MemoryStream(record.BinaryData))
{
using (var zipEntryStream = zipEntry.Open())
{
//Copy the attachment stream to the zip entry stream
originalFileStream.CopyTo(zipEntryStream);
}
}
}
}
}
Response.ClearContent();
Response.ClearHeaders();
Response.BinaryWrite(compressedFileStream.ToArray());
Response.AppendHeader("Content-Disposition", "Attachment; filename=result.zip");
Response.Flush();
Response.Close();
zipArchive.Dispose();
//How Do I Prompt for open or save?
}
}
Are you sure the BinaryWrite is getting a valid ByteArray?
In any case here's a tested method to output a file to the Response with the typically needed headers for binary attachments:
public bool WriteFile(byte[] byteContent, DateTime dtTimeStamp, string urlFilename)
{
HttpContext context = HttpContext.Current;
HttpResponse response = context.Response;
response.Clear();
response.ClearHeaders();
response.ClearContent();
response.BufferOutput = true;
response.AppendHeader("Content-Length", byteContent.Length.ToString());
response.AppendHeader("Content-Type", "application/octet-stream");
response.AppendHeader("Last-Modified", dtTimeStamp.ToString("R"));//Last-Modified Wed, 28 Aug 2013 10:16:46 GMT
response.AppendHeader("Content-Disposition", "inline; filename=\"" + urlFilename + "\"");
response.BinaryWrite(byteContent);
response.Flush();
// Prevents any other content from being sent to the browser
response.SuppressContent = true;
context.ApplicationInstance.CompleteRequest();
return true;
}
Related
My files are in blob storage.So how i can download the multiple files from folder as zip
I am trying this code from some time it is working but not giving me output.Means its not starting the zip download.:
string zipFileName = "MyZipFiles.zip";
using (var zipOutputStream = new
ZipOutputStream(HttpContext.Current.Response.OutputStream))
{
zipOutputStream.SetLevel(0);
HttpContext.Current.Response.BufferOutput = false;
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + zipFileName);
HttpContext.Current.Response.ContentType = "application/zip";
foreach (var filePath in fileUrl)
{
var filename = Path.GetFileName(filePath.filename);
var filebytes = filePath.filebyte.BlobByteArray;
var fileEntry = new ZipEntry(Path.GetFileName(filePath.filename))
{
Size = filebytes.Length
};
zipOutputStream.PutNextEntry(fileEntry);
zipOutputStream.Write(filebytes, 0, filebytes.Length);
}
zipOutputStream.Flush();
zipOutputStream.Close();
}
My file url contains:
foreach (var item in obj)
{
em = new FileUrlForbyte();
em.filename = item.FileName;
em.url = objBlobHelper.GetFileByFileNameMultiple(item.ContainerName, item.SubFolderName + "/" + item.FileName, DateTime.Now.AddMinutes(2));
em.filebyte = objBlobHelper.DownloadFileByFileNameForAdobe(item.ContainerName, item.SubFolderName + "/" + item.FileName);
fileUrl.Add(em);
}
FOr more clarity:filePath contains:filename,fileurl and file byte:
[Route("api/Blob/getMultipleFileFromBlobByURI")]
[HttpGet]
public System.Web.Mvc.FileResult getMultipleFileFromBlobByURI(string containerName)
{
List<BlobStorageModel> obj = new JavaScriptSerializer().Deserialize<List<BlobStorageModel>>(containerName);
try
{
BlobHelper objBlobHelper = new BlobHelper(apiPrincipal);
List<FileUrlForbyte> fileUrl = new List<FileUrlForbyte>();
FileUrlForbyte em = new FileUrlForbyte();
foreach (var item in obj)
{
em = new FileUrlForbyte();
em.filename = item.FileName;
em.url = objBlobHelper.GetFileByFileNameMultiple(item.ContainerName, item.SubFolderName + "/" + item.FileName, DateTime.Now.AddMinutes(2));
em.filebyte = objBlobHelper.DownloadFileByFileNameForAdobe(item.ContainerName, item.SubFolderName + "/" + item.FileName);
fileUrl.Add(em);
}
// Here we will create zip file & download
string zipFileName = "MyZipFiles.zip";
var fileName = string.Format("{0}_ImageFiles.zip", DateTime.Today.Date.ToString("dd-MM-yyyy") + "_1");
var tempOutPutPath = System.Web.HttpContext.Current.Server.MapPath(Url.Content("/TempImages/")) + fileName;
try
{
using (var zipOutputStream = new ZipOutputStream(HttpContext.Current.Response.OutputStream))
{
zipOutputStream.SetLevel(9);
byte[] buffer = new byte[4096];
HttpContext.Current.Response.BufferOutput = false;
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + zipFileName);
HttpContext.Current.Response.ContentType = "application/zip";
foreach (var filePath in fileUrl)
{
var filename = Path.GetFileName(filePath.filename);
var filebytes = filePath.filebyte.BlobByteArray;
var fileEntry = new ZipEntry(Path.GetFileName(filePath.filename))
{
Size = filebytes.Length
};
zipOutputStream.PutNextEntry(fileEntry);
zipOutputStream.Write(filebytes, 0, filebytes.Length);
}
zipOutputStream.Finish();
zipOutputStream.Flush();
zipOutputStream.Close();
}
byte[] finalResult = System.IO.File.ReadAllBytes(tempOutPutPath);
if (System.IO.File.Exists(tempOutPutPath))
System.IO.File.Delete(tempOutPutPath);
if (finalResult == null || !finalResult.Any())
throw new Exception(String.Format("No Files found with Image"));
return new System.IO.File(finalResult, "application/zip", fileName);
}
catch (Exception)
{
throw;
}
}
catch (Exception)
{
throw;
}
}
Thanks #Sudheer for your inputs in the comments.
I am trying this code from some time it is working but not giving me output.Means its not starting the zip download.: string zipFileName = “MyZipFiles.zip”;
Referring to the above comment I have tried the below code and was able to execute it successfully.
CloudStorageAccount storage_Account = CloudStorageAccount.Parse(storageAccount_connectionString);
CloudBlobClient blob_Client = storage_Account.CreateCloudBlobClient();
CloudBlobContainer container = blob_Client.GetContainerReference(container_Name);
CloudBlockBlob cloudBlockBlob = container.GetBlockBlobReference(filename);
Stream file = File.OpenWrite(#"C:\Tools\" + filename);
cloudBlockBlob.DownloadToStream(file);
Console.WriteLine("Download completed!");
Then, I have created a container in my storage account as shown below.
We need to upload the zip file into the above folder and run the given code.
Now I can see that I can access my container and download the zip file located inside the container.
I have a problem with get files to zip.
The web service is giving me the right files, for example www.XYP.com/GetFile?=1234 , when I open the Url, automaticly downloads the file, but after I zip this document its not correct files, which are destroyes
My code is here:
private void CreatedZipFile(Dictionary<int, string> listChecked)
{
using (MemoryStream memoryStream = new MemoryStream())
{
using (ZipArchive archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true))
{
foreach (KeyValuePair<int, string> kvp in listChecked)
{
ZipArchiveEntry demoFile = archive.CreateEntry(kvp.Key.ToString() + kvp.Value.ToString() + ".pdf");
string strDownLoad = System.Configuration.ConfigurationManager.AppSettings["GETFILESERVICES"] + kvp.Key.ToString();
WebService control = new WebService();
var clientnew = new System.Net.WebClient().DownloadData(strDownLoad);
using (var entryStream = demoFile.Open())
{
using (StreamWriter streamWriter = new StreamWriter(entryStream))
{
streamWriter.BaseStream.Write(clientnew, 0, clientnew.Length);
streamWriter.Close();
}
}
}
}
byte[] bytesInStream = memoryStream.ToArray();
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "attachment; filename=file.zip");
Response.Buffer = true;
Response.Clear();
Response.BinaryWrite(bytesInStream);
Response.Flush();
//MemoryStream.WriteTo(Response.OutputStream); //works too
Response.End();
}
}
Thanks for help or any advice,
I am using the following code to download a set of files stored in my database by putting them into a zip file:
SqlDataAdapter adp = new SqlDataAdapter("select FILE_NAME, FILE, content_type from tbl where id = " + "168", Configuration.getSQLConnString("ConnStr"));
DataTable dtFiles = new DataTable();
adp.Fill(dtFiles);
if (dtFiles.Rows.Count > 0)
{
using (var zipStream = new ZipOutputStream(Response.OutputStream))
{
foreach (DataRow dr in dtFiles.Rows)
{
byte[] bytes;
string fileName, contentType;
fileName = dr["File_Name"].ToString();
bytes = (byte[])dr["File"];
contentType = dr["Content_Type"].ToString();
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.AddHeader("Content-Disposition", "attachment; filename=Files.zip");
Response.ContentType = "application/zip";
byte[] fileBytes = bytes;
ZipEntry fileEntry = new ZipEntry(ZipEntry.CleanName(fileName));
fileEntry.Size = fileBytes.Length;
zipStream.SetLevel(3);
zipStream.PutNextEntry(fileEntry);
zipStream.Write(fileBytes, 0, fileBytes.Length);
zipStream.CloseEntry();
}
zipStream.Flush();
zipStream.Close();
}
}
The zip file gets generated and is downloaded and it also shows me a size of about 2Mb but then when I extract the zip file it shows me the following error:
Can someone please point out what is that I am doing wrong?
ZipOutputStream did not help me at all; tried breaking my head over it but nothing helped. Then I came across this library called DotNetZip and here's the code to download files stored in the database into a zip file:
using (ZipFile zip = new ZipFile())
{
foreach (DataRow dr in dtResumes.Rows)
{
zip.AddEntry(dr["File_Name"].ToString(), (byte[])dr["Resume"]);
}
zip.Save("C:\\Test\\MyZipFile.zip");
}
i am using ICSharpCode.SharpZipLib.Zip to compress the files & download zip file, and here using SQL File stream to store any kind of file(any amount of GB).
Then, how can i zip the files from sql file stream and get download...
And i tried something like below, which is throwing an exception "size was 845941, but I expected 16 at ICSharpCode.SharpZipLib.Zip.ZipOutputStream.CloseEntry()".How to solve this...
string zipFileName = "ZipName.zip";
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "fileName=" + zipFileName);
byte[] buffer = new byte[4096];
ZipOutputStream zipOutputStream = new ZipOutputStream(Response.OutputStream);
zipOutputStream.SetLevel(3);
string cs = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
foreach (Filec file1 in Files)
{
StreamModel model123 = new StreamModel();
const string SelectTSql = #"
SELECT FileData.PathName(), GET_FILESTREAM_TRANSACTION_CONTEXT(), FileType
FROM MyFiles WHERE FileId = #FileId";
using (TransactionScope ts = new TransactionScope())
{
using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(cs))
{
conn.Open();
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(SelectTSql, conn))
{
cmd.Parameters.Add("#FileId", System.Data.SqlDbType.Int).Value = Convert.ToInt32(file1.FileId);
using (System.Data.SqlClient.SqlDataReader rdr = cmd.ExecuteReader())
{
rdr.Read();
model123.serverPath = rdr.GetSqlString(0).Value;
model123.serverTxn = rdr.GetSqlBinary(1).Value;
model123.filetype = rdr.GetSqlString(2).Value;
rdr.Close();
}
}
}
ZipEntry zipEntry = new ZipEntry(ZipEntry.CleanName(file1.FileName));
zipEntry.Size = model123.serverTxn.Length;
zipOutputStream.PutNextEntry(zipEntry);
byte[] buffer3 = new byte[4096];
using (System.Data.SqlTypes.SqlFileStream sfs = new System.Data.SqlTypes.SqlFileStream(model123.serverPath, model123.serverTxn, FileAccess.Read))
{
int bytesRead;
while ((bytesRead = sfs.Read(buffer3, 0, buffer3.Length)) > 0)
{
zipOutputStream.Write(buffer3, 0, bytesRead);
}
sfs.Close();
}
zipOutputStream.CloseEntry(); // at this line throwing an exception.
ts.Complete();
}
}
zipOutputStream.Close();
Response.Flush();
Response.End();
After Understanding each line of code, i figured out the solution..
1) "zipEntry.Size = model123.serverTxn.Length;" this line is causing the exception as "size was 845941, but I expected 16"..because "model123.serverTxn.Length" is not the complete size of the file., So i changed this to "sfs.Length" which is SqlFileStream length.
2) zipOutputStream level is set maximum as "zipOutputStream.SetLevel(9)" because, i am zipping the large size files here like videos..
3) And TransactionScope has to be more, other wise the complete file(large files more than 500mb) is not going to be downloaded hence we will see file damaged error message after downloading..
string zipFileName = "ZipName.zip";
Response.ContentType = "application/zip";
Response.AddHeader("content-disposition", "fileName=" + zipFileName);
byte[] buffer = new byte[4096];
ZipOutputStream zipOutputStream = new ZipOutputStream(Response.OutputStream);
zipOutputStream.SetLevel(9); // Point 2
try
{
string cs = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
foreach (Filec file1 in Files)
{
StreamModel model123 = new StreamModel();
const string SelectTSql = #"SELECT FileData.PathName(), GET_FILESTREAM_TRANSACTION_CONTEXT(), FileType
FROM MyFiles WHERE FileId = #FileId";
using (TransactionScope ts = new TransactionScope(TransactionScopeOption.Required,
new TransactionOptions { Timeout = TimeSpan.FromDays(1) })) // Point 3
{
using (System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection(cs))
{
conn.Open();
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(SelectTSql, conn))
{
cmd.Parameters.Add("#FileId", System.Data.SqlDbType.Int).Value = Convert.ToInt32(file1.FileId);
using (System.Data.SqlClient.SqlDataReader rdr = cmd.ExecuteReader())
{
rdr.Read();
model123.serverPath = rdr.GetSqlString(0).Value;
model123.serverTxn = rdr.GetSqlBinary(1).Value;
model123.filetype = rdr.GetSqlString(2).Value;
rdr.Close();
}
}
}
using (System.Data.SqlTypes.SqlFileStream sfs = new System.Data.SqlTypes.SqlFileStream(model123.serverPath, model123.serverTxn, FileAccess.Read))
{
ZipEntry zipEntry = new ZipEntry(ZipEntry.CleanName(file1.FileName));
zipEntry.Size = sfs.Length; // Point 1
zipOutputStream.PutNextEntry(zipEntry);
int bytesRead;
while ((bytesRead = sfs.Read(buffer, 0, buffer.Length)) > 0)
{
if (!Response.IsClientConnected)
{
break;
}
zipOutputStream.Write(buffer, 0, bytesRead);
Response.Flush();
}
sfs.Close();
}
ts.Complete();
}
zipOutputStream.CloseEntry();
}
zipOutputStream.Finish();
zipOutputStream.Close();
Response.Flush();
Response.End();
}
catch (Exception ex)
{
TempData["ErrorMessage"] = "Oohhhh! Exception Occured(Error)...";
}
I have read several blog posts and questions about this, but I can't get this working. It just downloads a corrupt ZIP archive that is <2KB. I feel like I'm missing something simple with streaming the PDF file to the Zip Archive?
using (var outStream = new MemoryStream())
{
using (ZipArchive archive = new ZipArchive(outStream, ZipArchiveMode.Create, true))
{
foreach (string Item in Items)
{
ReportParameter[] prms = new ReportParameter[1];
prms[0] = new ReportParameter("Item", Item, false);
rv.ServerReport.SetParameters(prms);
rv.ShowParameterPrompts = false;
rv.ServerReport.Refresh();
byte[] bytes = rv.ServerReport.Render("PDF", null, out mimeType, out encoding, out extension, out streamids, out warnings);
string fileName = Item + ".pdf";
var fileInArchive = archive.CreateEntry(fileName);
using (var entryStream = fileInArchive.Open())
using (var fileToCompressStream = new MemoryStream(bytes))
{
fileToCompressStream.CopyTo(entryStream);
}
}
}
outStream.Position = 0;
Response.ClearContent();
Response.ClearHeaders();
Response.AppendHeader("content-disposition", "attachment; filename=PDFReports_" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".zip");
Response.Write(outStream);
}