I'm trying to write text to file on server from winform desktop application
string path = "http://www.site.info/doc.txt";
To use path:
System.Web.HttpContext.Current.Server.MapPath(path);
also I tried this way:
System.Web.Hosting.HostingEnvironment.MapPath(path);
to write text into the text document:
using (StreamWriter _testData = new StreamWriter(Server.MapPath("~/doc.txt"), true))
{
_testData.WriteLine("TEXT");
}
Seems like I'm doing something wrong,
name Server "does not exists in current context".
Not sure how to use Server.MapPath.
it is in References as System.Web not System.Web.dll, not sure, but it must be same, and in using as System.Web;
Also I am using System.Net; so maybe I can do it with WebClient.
You are trying to modify the file on server which the server wont allow as this could be misused and harm the server. You can update the file through the website hosting this text file.
The Server.Map path should be used in the website where you want to modify the file. If the file is asp.net web form website then you can make a aspx page that will modify file for you. If it is MVC then you will need a Action method in Controller to modify file for you.
If you want you own modified copy then you can download it and save it locally the Winform application as suggested by Sadiq. You can also upload the file by again your server side must allow this.
Why are you using Server.MapPath in a winform desktop application.
Download the file using something like this:
WebClient webClient = new WebClient();
var filearray = webClient.DownloadData(path);
and then write it to your local after modification (if needed) using
File.WriteAllBytes(savefilePath, filearray);
And then upload using webClient.UploadData(address, filearray).
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 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
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)
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.
i have a URL and when i load it up in a browser it recognizes it as a csv file and pops up excel "do you want to open". I want to do this programatically so i can have a winforms app use that url and parse the csv file directly.
what is the quickest way to do this?
EDIT: i tried using WebClient and i am getting the folowing error:
"The remote server returned an error: (500) Internal Server Error."
I don't see why something like this wouldn't work (in C#):
// Download the file to a specified path. Using the WebClient class we can download
// files directly from a provided url, like in this case.
System.Net.WebClient client = new WebClient();
client.DownloadFile(url, csvPath);
Where the url is your site with the csv file and the csvPath is where you want the actual file to go.
If you have a WinForms app, you can use a System.Net.WebClient to read the data as a string.
It will read the entire csv file as a string, but you can write it out or parse it at will.
If you want to just whip something together I would suggest using a scripting language and some bash. Just use wget or something similar to get the file and some scripting language to parse it. You could even use php to parse it once you had the file because I know that php has the following function which is very nice: http://php.net/manual/en/function.fgetcsv.php
I would suggest doing it this way because it is easier, this will certainly let you parse it easily enough though I don't know what you want to do with it from there but the worlds your oyster.
The following code works for me but I am running Open Office. I have not tested it with Excel.
The hacky bit is to rename the local copy of the file to *.xls so that Windows will launch Excel by default, if you leave the file extension as CSV, Windows will launch Notepad by default.
String url = "http://www.example.com/test.csv";
String localfile = "test.xls";
var client = new WebClient();
client.DownloadFile(url, localfile);
System.Diagnostics.Process.Start(localfile);