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
Related
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("\", "");
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
I'm trying to download and run a file to the client machine. The client is aware of that.
It's a ttkgp file that's dynamicly generated.
I've tried using Processs.Start() that worked fine on my local machine (first saved the file to C:\ then lunched it), but it's not working from the server. It's not my server but a hosted one. They are trying to help but no luck so far.
I've seen this code:
public void ProcessRequest(HttpContext context)
{
string fileName = context.Request.QueryString["filename"];
FileInfo fi = new FileInfo(fileName);
context.Response.ContentType = "application/x-rar-compressed";
context.Response.AppendHeader("Content-Disposition",
string.Format("attachment; filename=download{0}", fi.Name));
context.Response.WriteFile(fileName);
context.Response.End();
}
But since I dont know what's "HttpContext context" is, I've no idea if it works.
Is it some server previlges need to be changed? or simply this code will do the trick?
Thank you
UPDATE (24.6.12): I'm nearly finished with the problem, all I need now is to know how to open an html page in a new tab / window and close it second later. Once I'm done, I'll post back here all the process, I'm sure it'll help other people.
UPDATE (26.6.12):
Here's what I've got:
The goal is to download an TTKGP file from asp.net webiste to local user machine and run it.
Step 1: generate the file with code behaind (c#) on the server (V)
Step 2: copy the file or it's content to user machine (X)
Step 3: run the file using JS (V)
Here's the thing: I CAN copy from a text file on the server to a text file on the user machine, but not from TTKGP files. It's strange because this are just text files just a different extantion.
The code for copying text files:
enter code here
function copyremotetxt() // works
{
// copy the txt file
var fso = new ActiveXObject("Scripting.FileSystemObject");
var newfile = fso.CopyFile("remote.txt", "C:\\Users\\***\\local.txt");
}
Perhaps I can change the file type on the user machine?
Notice 1: I know that's a security issue, the site is just for known user not the open public
Notice 2: I know there are better ways to get the task done, but there are strict limitaions on many things
Thanks for those how can help!!
This code will do the trick. It will prompt the client to download and save the file on his computer at the location he decides. What happens next with this file is the client's decision, not yours. He might simply close the Save As dialog, interrupt the download, delete the file, ... It's up to him.
Another remark: this code is extremely dangerous because it allows the client to pass any filename he wants as query string parameter and download it. So he could read absolutely all files on the server which is probably not something that you want to happen.
Ok, this need a different aproach.
I'll try using JavaScript do read the file on the server, rewrite it in the user machine and activate it. Any clues would be grate! For a start, how to I read file in JS? I'm new to it.
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
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.