I am building a web application using WCF. I need to full path of a file to open and upload it to web service.
What I am doing is first opening a file by its full path, then take stream of it and finally upload it to service. My code is below
string Path = Server.MapPath( FileUpload1.PostedFile.FileName);
System.IO.Stream fileStream = File.OpenRead(#Path);
I can't get full path of the file for security reasons.
How can I read the file that users select?
Server.MapPath(FileUpload1.FileName) is the path to a file on the server. You cannot get the full path of the client machine of the file Using the FileUpload.
There is no need for the full client path to access it. You can use the FileBytes
property in order to read the contents of uploaded file.
As others have already suggested there's no reason you need the file path to the client in order to save a file on your server. In the case you need some clearer explanation for this, then please refer to these answers:
https://stackoverflow.com/a/3077008/2196675
https://stackoverflow.com/a/1130718/2196675
Related
I am uploading a file with a .rvt extension. using filestream and then UploadAsync(). I can upload and download the file from a folder I generate on the Dropbox remote server using a c# desktop app called from a winform. I can also rename the file with the correct extension and it is formed correctly. below is the code to achieve that. How can I rename the file or create the file correctly on dropbox so it has the correct extension? The properly formed file is of little use without an extension ".rvt".
using (var fileStream = File.Open(localPath, FileMode.Open))
{
if (fileStream.Length <= ChunkSize)
{
await dbx.Files.UploadAsync(remotePath, WriteMode.Overwrite.Instance, body: fileStream);
}
The next issue is if I try to upload a file say 24mb or greater using the same code it returns an error?
cheers.
When you upload a file to Dropbox, it will have whatever file extension you specify, but you do need to specify it yourself.
In your code, the remotePath value should be the entire path and name, including file extension, you want for the uploaded file. For example, to upload a file "myfile" with extension "rvt" to a folder named "myfolder", the value should be "/myfolder/myfile.rvt".
The easiest way to upload a large file using C# appears to be via a WebClient object:
Asynchronous Upload of Large File Using HttpWebRequest in C#.Net
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
I'm trying to download a file using the Tamir SSH library. I'm able to connect to the remote FTP site, upload files to the site, but I'm getting exceptions when trying to download. I've given IIS_IUSRS full control of the local directory as well as ASPNET. I've tested an I'm able to create a text file in the same local directory I'm trying to download to. Any ideas?
string SFTP_HOST = ConfigurationManager.AppSettings["AccentivFtpHost"];
string SFTP_USERNAME = ConfigurationManager.AppSettings["AccentivFtpUsername"];
string SFTP_PASSWORD = ConfigurationManager.AppSettings["AccentivFtpPassword"];
Sftp client = new Sftp(SFTP_HOST, SFTP_USERNAME, SFTP_PASSWORD);
client.Connect(22);
client.Get("test.txt", "c:\\test.txt");
You probably lack a '/' character in the file directory. You may need to put it either in the Get function call before the "test.txt" like "/test.txt" or at the end of your AccentivFtpHost value in the app config file.
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.
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.