DotNetZip library "Access to the path denied" - asp.net

I am trying to create a zip file and save it using DotNetZip library in ASP.NET application.
But for some reason i get a
Access to the path is denied
error when i try to save it.
I changed the TempFileFolder to another folder and have given permissions to it. Still no luck.
Dim zipFile As New ZipFile()
zipFile.AddFile(filePath)
Dim tempFilePath As String = "Report.zip"
zipFile.TempFileFolder = "D:\Temp\"
zipFile.Save(tempFilePath);
I found a question but the answer did not help me.
From the above question, one answer mentioned:
Also, the tempFilePath in your example doesn't include a full path, could it be that it is trying to save the ZIP into a different folder from the one you are expecting (and have assigned permissions to)?
How to figure out to which folder it is trying to save even though I mentioned TempFileFolder as D:\temp\?
Any thoughts?

Since you said you 'gave permissions' I'm assuming that you provided the account(s) which run the ASP.NET and IIS processes file Read/Write permissions to the folder where you're trying to save this file.
The 'Temp File Folder' is just what its name describes: a temporary file folder. It's a holding place in case the library needs to do some file I/O. it's not a base file.
Modify the code to provide a fully qualified path name to save the file to:
Dim zipFile As New ZipFile()
zipFile.AddFile(filePath)
Dim tempFilePath As String = "D:\Temp\Report.zip"
zipFile.TempFileFolder = "D:\Temp\"
zipFile.Save(tempFilePath)

Check if the file is not Read Only

Related

Why the file stream is reading wrong path?

I am trying to access textfile present in the folder inside the solution.
Dim fs As New FileStream("../CMMS/Webservices_URL.txt", FileMode.Open, FileAccess.Read)
but it always picks c drive even though the solution is somewhere else. why it's happening?
Folder1/Folder2/CMMS/Webservices_URL.txt is the actual path but it picks c:/CMMS.
To get the physical path of a file in Web Forms you need to call Server.MapPath("~/Folder1/Folder2/CMMS/Webservices_URL.txt")
you can find the details here:
https://learn.microsoft.com/en-us/dotnet/api/system.web.httpserverutility.mappath?view=netframework-4.8

asp.net mvc published how can ı save file in iss server and how can fınd thisfolder path

Im writing a asp.net mvc project it basically create excess and save server im try to do that and its work on my local like this gif user set 2 dates and ım fılter my db sources using this two date and after that ım create excel files and save in folder "Reports" it's perfectly work in my local but its doesn't work published folder doesn't have report folder and ıf ım try create excel in iss its created but ı dont know its saved or if saved save where?
Im save my my excel files like this
string path = Path.Combine(Server.MapPath("~/"), ("Reports\\" + name));
FileInfo excelFile = new FileInfo(path);//new FileInfo(#"C:\Users\amir\Desktop\test.xlsx");
excel.SaveAs(excelFile);
and its donsnt work in Release dont save this files in this folder also this folder(Reports) does not exist and also ım create manually but ıts donst work
after that ım take error from my download function like this
Could not find file 'G:\PleskVhosts\ngsdev.net\httpdocs\Reports\Report_1_1_2020-2-19-04_PM.xls'.
so i try to change my path like this
string path = "G:\\PleskVhosts\\ngsdev.net\\httpdocs\\Reports\\" + name;
and its doesn't work so how can ı do this like my locally any idea or solution is welcomed thanks for help.

Response.WriteFile adding directory path where it shouldnt

I have a web page that is displaying a PDF file with the following code:
Response.Clear();
strFilePath = Server.HtmlDecode(Request.QueryString["filename"]);
Response.ContentType = "application/pdf";
Response.WriteFile(strFilePath);
The filename got from Server.HtmlDecode() is "\FileServer\shared\faxqueue\fax.pdf"
However an exception is thrown for directory not found and it says that it cant find the file. It also says in the exception that it is looking for: "C:[Website Root Folder]\FileServer\shared\faxqueue\fax.pdf"
This means that it has appended the filename given to the folder where the website code is located.
How can I stop it from using the website root?
Thanks
That is true because you ask it to do so.
It is a bad idea to pass in the direct file name using the query parameters.
You can of course create a direct path to the file you are using instead of this relative path:
string absolutePath = Path.Combine(#"C:\yourRootFolder", strFilePath);
Response.WriteFile(absolutePath);
But as said, I warn you for the security risks! You have to grant the IIS application pool user access to the folder you specify here. Your files can be easily hijacked by passing in something like:
..\..\..\Windows\anysecurefile.txt

ASP .NET invalid path

I have a web page which prompts the user for an excel file using the fileupload control. What it then does is read the file into a datatable using an OleDbConnection and then runs other code with that data. When I test in Visual Studio, it works fine. For example, I can look up a file 'g:\myfiles\upldtest.xls', it finds the file, reads it and the code works. When I try to run it on our web server, I get an error when it tries to create an OleDBConnection saying It is trying to create an OleDbConnection and the path 'g:\myfiles\upldtest.xls' is invalid.
I tried to use ManagementObjectSearcher to convert the connection string path to UNC (\\MyDataServer\myfiles instead of g:\myfiles). When I test it, it shows the correct path but when I upload the page to the web server, I still get the path 'g:\myfiles\upldtest.xls' is invalid.
The code I use to determine the required file name is this
string tname = FileUpload1.PostedFile.FileName; //the file name and path
string gname = tname.Substring(tname.LastIndexOf("\\") + 1); //The path name
Any ideas what I am missing? My contract requires me to use VS2005 and .NET framework 2.0 so, I can't use anything newer. Thanks in advance for the assistance.
HttpPostedFile.FileName returns the fully qualified name of the file on the client machine.
You need to call SaveAs() to actually save the file on the server:
using System.IO;
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string filepath = Path.Combine(#"X:\Your\Own\Upload\Folder", filename);
FileUpload1.PostedFile.SaveAs(filepath);
// Now use `filepath` as your data source.
IIS might have already written the file in a temporary location to save memory, but since you can't (and shouldn't) access that location, it makes no difference.
You should also be aware of cross-browser issues. IE sends the whole path to the server on file upload, while Firefox/Chrome do not.

How can you delete a file in the application directory?

We are writing log files to File.applicationDirectory and want to clean out old log files. The question is how do you delete files out of that directory? When I try and delete old log files out of this directory it gives a security exception. Is there anyway around this?
I know you are not supposed to write to File.applicationDirectory, but we are writting are logs there anyways. So please don't just say don't do that!
Thanks,
Ryan
Another trick, for security workaround
var fileLoc:File= File.applicationDirectory.resolvePath("some.log");
var file:File=new File(fileLoc.nativePath);
fileStream.open(file,FileMode.WRITE);
The second file object can alter the file withot any problem.
File.applicationDirectory is a read only directory.
http://livedocs.adobe.com/flex/3/langref/flash/filesystem/File.html
I found the answer shortly after posting. I was looking in FileTarget of how they write the log file into the application directory and found this gem:
var logFile:File = this._logDirectory.resolvePath(filename);
logFile = new File(logFile.nativePath); // Hack to get around SecurityError if log directory exists within the application directory
So you just specify the full native path and not a relative path from the application directory and you can do whatever you want. Interesting security model!

Resources