in a project asp.net, I generate invoices with active reports and I save them in pdf format. there I want to create a zip file dynamically and add the pdf invoices then downloading this file zip in client side. help me please.
string zipFileName = "a.zip";
using (var zip = new ZipFile())
{
System.IO.File.Delete(fileDir + "/" + zipFileName);
zip.AddDirectory(fileDir + "/");
zip.CompressionLevel = CompressionLevel.Level5;
zip.CompressionMethod = CompressionMethod.BZip2;
zip.Save(fileDir + "/" + zipFileName);
}
Related
Want to duplicate a file in a local directory with a new file extension. I don't see any documentation for duplicating a file with the File Object.
I see the ability to File.copy(), etc but nothing having to do with duplicating or saving without a dialog box with a new name and extension.
var targetFile = new File('myFile');
targetFile.saveDlg('newFileName' + 'extension');
To do it by code, you must be more explicit. You can try this:
function duplicateFile(path) {
var content, extension, file, fileOk, name, newFile, newPath;
file = new File(path);
if(!file) {
return
}
fileOk = file.open('r');
if(fileOk){
//Get file extension
name = file.name.split('.');
extension = name.pop();
name.join('.');
//Creating new file
//Becareful with the name, you must to check that a file with the same name doesn't exists
//if you don't want to overwrite it.
name = name + '_copy.' + extension
newPath = file.parent.fsName + '/' + name
newFile = new File(newPath);
fileOk = newFile.open('w');
//Writing content to new file
if (fileOk) {
newFile.write(content);
newFile.close(); //Remember to close the files
}
file.close()
}
}
I am using DotNetZip.dll(1.10.1) for creating zip files(media files), Files get zipped but when I try to extract it using WinRaR or with windows extract folder option, I am getting "the archive is unknown format or damaged"
List<string> fileNames = new List<string>();
using (ZipFile zip = new ZipFile())
{
//zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression;
zip.ParallelDeflateThreshold = -1;
zip.Encryption = EncryptionAlgorithm.None;
zip.AddFiles(fileNames.AsEnumerable(),"");
zip.Save(zipSavePath);
}
CRC32 of zipped file is 00000000.
I am using .net framework 4.0 and web forms.
Can you please let me what wrong am I doing
Thanks
Im trying to search and download the files uploaded to the database,I can able to retrieve the files from the database.
Downloading single file works but cant download multiple files at a time,I have read about downloading as a zip file,Can anyone help??????
using (sampledbase dbcontext = new sampledbase ())
{
ZipFile zip = new ZipFile();
long id = Convert.ToInt64(Request.QueryString["id"]);
System.Nullable<long> fileid = id;
var query = dbcontext.searchfile(ref fileid );
foreach (var i in query)
{
byte[] binarydata = i.Data.ToArray();
Response.AddHeader("content-disposition", string.Format("attachment; filename =\"{0}\"", i.Name));
Response.BinaryWrite(binarydata);
Response.ContentType = i.ContentType;
Response.End();
}
}
I see that you are using ZipFile so you are in the right direction. In order to zip your files, you can save your files in a directory and zip the directory using this code :
string startPath = #"c:\example\start";
string zipPath = #"c:\example\result.zip";
ZipFile.CreateFromDirectory(startPath, zipPath);
In your case, look at the data type returned by dbcontext.searchfile() and save the content to a directory using something like StreamWriter. Looking at your code, the type seems to be a byte[], you can see this link to learn how to save the bytes in a file : Write bytes to file
Another solution is to zip directly the bytes[] : Create zip file from byte[]
In my ASP.NET WEbForms app, I want to create a folder and save files in it. In my project I have a folder named CRMImages/Projects. I want to create a sub folder in Projects folder & save images from their. Currently I retrieve images from CRMImages/ as the parent folder.
This is my code that I have on code-behind :
try
{
string pathToCreate = "~/CRMImages/Projects/" + item.ProjectId;
string myFileName = "";
if (!Directory.Exists(Server.MapPath(pathToCreate)))
{
DirectoryInfo di = Directory.CreateDirectory(pathToCreate);
var user = System.Security.Principal.WindowsIdentity.GetCurrent().User;
var userName = user.Translate(typeof(System.Security.Principal.NTAccount));
System.Security.AccessControl.DirectorySecurity sec = di.GetAccessControl();
sec.AddAccessRule(new System.Security.AccessControl.FileSystemAccessRule(userName,
System.Security.AccessControl.FileSystemRights.Modify,
System.Security.AccessControl.AccessControlType.Allow));
di.SetAccessControl(sec);
Directory.CreateDirectory(pathToCreate);
System.Diagnostics.Debug.WriteLine("FOLDER CREATED PATH : " + di.FullName);
myFileName = pathToCreate + "/projectLogo.png";
System.Diagnostics.Debug.WriteLine("PATH To Save Logo File & NAME : " + myFileName);
/*
if (File.Exists(item.ProjectLogoUrl) ) {
FileUpload projLogoUpload = new FileUpload();
if (projLogoUpload.HasFile) {
myFileName = pathToCreate + "/projectLogo.png";
projLogoUpload.SaveAs(myFileName);
}
// panFileBtn.SaveAs(filePath);
} */
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("EXCEPTION While SAving File : " + ex.Message + "\n *** STACK" + ex.StackTrace);
}
The code executes, but I don't see the folder created in my project folder. Lets say the value of item.ProjectId is "EMP3", the logs that I see on the above code execution is :
FOLDER CREATED PATH : C:\Program Files (x86)\IIS Express\~\CRMImages\Projects\EMP3
PATH To Save Logo File & NAME : ~/CRMImages/Projects/EMP3/projectLogo.png
I checked in IIS Express folder & there this full path is created. Can you say why is it saving in IISExpress & how to create the folder in the /CRMImages/Projects folder that already exists in my project !!
Any help is highly appreciated.
Thanks
You have to replace below line
DirectoryInfo di = Directory.CreateDirectory(pathToCreate);
With
DirectoryInfo di = Directory.CreateDirectory(Server.MapPath(pathToCreate));
I am using dynamic ajax file upload control.
AjaxControlToolkit.AsyncFileUpload()
While i am trying to upload file, in page load the
value of Request.ServerVariables["QUERY_STRING"] is, "AsyncFileUploadID=MainContent_flbFileUpload1&rnd=01846502097323537".
Can anyone tell me, why this happens?
In AsyncFileUpload.pre.js file found in ajaxcontroltoolkit.zip and in _onload: function (e) { .... } has below sample lines which create the resulting postback URL as:
mainForm.action = url + 'AsyncFileUploadID=' + this.get_element().id + '&rnd='
+ Math.random().toString().replace(/\./g, "");
mainForm.target = this._iframeName;