Mapping A Filename To Paths on A Server Asp.net - c#

I have a problem with my code. My code is using the fileupload control to browse for a filename when you add a filename it processes it and the code runs fine on when it lives on local host, but when I put the code on our prodution server it cannot find the filenames listed by user.
For example if I use the upload control to browse to
B:\MIS\CH Intive\RPTTOFL_3.csv and the code lives on my localhost which know what that file path means it works, but if the code is moved to a production server it may or maynot know what B:/ is or B:/ maybe mapped to something else.
Even if I am browsing to a file on my C drive it will work on if the code is on the machine that the C drive is on, but it will not work if the code is on another machine because obviously that file wouldnt be on that C drive.
Private Function CSV2DataTable(ByVal filename As String) As DataTable
Using MyReader As New _
Microsoft.VisualBasic.FileIO.TextFieldParser(filename)
MyReader.TextFieldType = FileIO.FieldType.Delimited
.
.
.
What can I do in asp.net to make the filename work correctly?
Ok lets say I get the filename and save it as so
FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
now I want to pass the filename to the function above for processing. Do I pass Server.MapPath("~/") + filename as the filename? Also when I am done what do I do to delete the file from the server?

It seems that you are mixing the client and server locations of the file. Before reading the uploaded file, the server-side code must save it on the server (client-side file location is mostly irrelevant at this point). From VS help on FileUpload class: "The code that you write to save the specified file should call the SaveAs method, which saves the contents of a file to a specified path on the server." The online help topic on FileUpload control has enough information (with examples) to achieve what you need.

Related

C# Upload Files on another partition of the server

I'm using FileUpload.SaveAs() function of C# to upload files to the server but I want to save the files on another partition. Let us say, save the files on Drive D of the server instead on the current drive which is Drive C. Please share your thoughts. Thanks is advance.
I have learned that using full path such as
FileUpload.SaveAs("D:\FileUpload");
will save the file outside the web server.
Check this out.
To simplify the question, how can I upload files on the other partition of the server that hosts my web app?
Based on the documentation from http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.saveas.aspx, the String filename is the full path name of the location to save. Meaning you should be able to do so e.g:
FileUpload.SaveAs("D:\where_you_want_to_save")
By the way what have you tried and what error did you get?
Looking at the example on MSDN, it would appear that .SaveAs() accepts a fully qualified file name as a parameter. You could potentially use a Path object to cleanly build a path for the file, or just specify one directly as a string:
uploader.SaveAs("d:\\someFolder\\someFile.ext");
Resolved this by using Virtual Directory of IIS and providing admin credentials for authentication

get full path through file uploader in Telerik

I am using telerik controls in asp.net
For uploading a file i am using, RadUpload
I gone through following links:
http://www.telerik.com/community/forums/aspnet-ajax/upload/get-full-path-from-uplad-control.aspx#1044702
http://www.telerik.com/community/forums/aspnet-ajax/async-upload/how-to-get-full-path-using-radasyncupload-control.aspx
Made Code As Follows:
for (int i = 0; i < RadUpload1.UploadedFiles.Count; i++)
{
string fileName= Server.MapPath( RadUpload1.UploadedFiles[i].GetName());
}
Its giving me path:
E:\WebBasedNewSoft\NewSoft\NewSoft\colnames.xlsx
this is the path where my solution files are stored.
I wanted to get path of file selected for upload.
Eg. if uploaded file is on c drive , it should give me path:
C:\colnames.xlsx
But its not giving me appropriate path.
What mistake am i making?
What should be appropriate line in for loop??
Please help me.
The file path will NEVER contain the path of the client's computers, because the software on the server is never allowed to take a peek inside the clients computer. If the server was alles to see inside the clients computer we would have a security issue.
The path you are getting is the local path at the server to where the file has been uploaded.

ASP.NET File.Exists returns false when file exists

File.Exists is returning false when the file actually exists.
I know it would return false if there's no read permissions, but I can't seem to get it right.
More info:
My FTP directory is organized like so:
Root
ImageSite
ImageDirectory
MySite
My .aspx.cs File that's calling File.Exists()
I tried uploading the .aspx.cs file with the same ftp user account as ImageSite & ImageDirectory. Didn't help.
I also tried replacing forward slashes with back slashes, just to be sure.
Here's my code - I hard-coded the file path to the complete ftp path of the image directory.
string serverUrl =#"\\fs4-n01\blah\blah\blah\ImageSite.com\web\content\Images\" + product.ImageUrl;
bool exists = File.Exists(serverUrl);
I also thought of impersonation. I am already using one identity impersonation in my web.config and it didn't seem to let me use both, so I placed this one under the subdirectory where my code file is, under tag. But it didn't seem to do it. (Either way, the owner of the file is the same as the image directory, as I mentioned.)
I'm using ASP.NET 4.0.
Any ideas would be appreciated.
I called my hosting provider (Rackspace) and was told the way .NET works is you cannot access files beyond the content directory for security reasons. (For ASP classic sites, it does let you, as of now.)

How to check if a file exists on the web server

I have a file stored on the server and the web page I am populating depends on the fatc that the file exists or not.
How do I test if the file is available on the server?
The file comes on the web page as:
http://main.server.com/PGT/Reports/ObjectsReport.xml
I have to test the existence of this file and if it is available I will display a link otherwise I want to hide the link.
The actual path to the server is
//main.server.com/inetpub/wwwroot/PGT/Reports/ObjectsReport.xml
but I don't have access to the server (and therefore to the file) on the network. I can only access it using the web page. Is there a way to test that the server has the file or not display the link? (hlObjectsReport.Visible = false;)
I have tried to use the following:
Uri validatedUri;
Uri.TryCreate(uri, UriKind.RelativeOrAbsolute, out validatedUri);
But it returns a valid address even if the file is not there.
Thanks
Tony.
use System.IO.File.Exists() (Documentation)
if(System.IO.File.Exists([path goes here]))
{
// do something
}
If you're not sure of the physical path, you can substitute the following for [path goes here] above:
Server.MapPath(/PGT/Reports/ObjectsReport.xml)
(Documentation)

Getting the full file path when using a file upload control

I wrote some code to upload files to amazon S3, if I put a full file path manually It successfully uploads the file from my computer. What I'm trying to do is use a file upload control and store the full path in a variable so that I can use it for my amazon method. Ive read everywhere it seems that the browser won't let you get the full file path for security reasons.
How can I get the full file path? Should I just store the files on my webserver and point my amazon method to the server path, and then use the file upload control to tell it what the filename is? I wish I could just do a straight shot to amazon...
First we have to save the file path and then we take it from
string filepath=Path.GetFullPath(UploadFile1.FileName.toString());
I came across this link which has a great tutorial and even gives you a working sample project. (this is different from the code that the .net SDK includes...) http://aws.amazon.com/articles/774?_encoding=UTF8&jiveRedirect=1
We can't take full path in HTML or JS as it violets security so whenever you try to see the path it shows fakepath
so to resolve this issue you can make a seprate folder and you can store the uploaded file there and in the code you can take that folders path as default and use it as a absolute path.
You can get the full path using Python Tkinter but it is limited for desktop app.

Categories