Path get extra backslashes when retrieved from database - asp.net

I'm working on a script that picks file from one location (source) and copies to another location (destination). The script is working file if the path is provided directly. But I want path to be retrieved from a database.
While storing path in database the path is #"D:\test" but when the path is retrieved it comes out as "#\"D:\test\""
I'm using ASP.Net as a platform for this. Please help me in resolving this issue.

use split() method to remove those extra backslsh
str = str.replaceAll("\", "\");
or
str = str.replace("\", "");

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

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

Get file path & filename with asp:FileUpload, don't want file... just path and name

I am developing a .NET intranet site which will enable the user to see a list of files (file details stored in DB) and link to the actual PDF/XML/XLS and open it... kind of like a table of contents for the network.
During data entry, the user enters various data about a document, then browses to the file on the network and selects it using the asp:FileUpload. The codebehind then saves the network path to the DB. There is alot of overhead here because i'm sending the file to the server but never use it.
Everything has been working fine until someone tries to use a large PDF file then I get the dreaded MAXIMUM REQUEST LENGTH EXCEEDED error... So I'm trying to find a solution here... I do not need the actual file.. just the path and filename.
I know not all browsers send the full path but our systems have older browsers so everything is working fine now, but will probably break soon.. which is another reason to find a different solution.
I've looked into Javascript to pull the path but that won't work...
Any other ideas? Other ways to just grab the path and filename? (besides manually typing it in to a Text field)
Thanks,
Todd.
This may help too
How to get the full path of a file from asp: file upload?
string filename = Path.GetFileName(FileUpload1.FileName);//file name
string path= Server.MapPath(filename);//path

DotNetZip library "Access to the path denied"

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

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.

Resources