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);
Related
I want to save a file from a http link to the local drive just temporarily in order to access it, this one is working so far and I'm getting the data but a need to write this data to a local file, for example to C:\Windows\temp\test.text, this file should be deleted afterwards.
WebClient client = new WebClient();
string url = "http://www.example.com/test.text";
var file = client.DownloadData(url);
could any one help me on this, thank you!
You cannot write a file on client machine due to security, Any program executing in the browser executes within the browser sandbox and has access to limited features like printer, cookies, etc.
You can write the data to file as a Response object to the client's browser. The Client has the choice of whether to save it or not to his machine.
I'm trying to download files using a list of urls. how would i go about downloading the files if your urls only end in the page where you would normally click the download button(it has a redirect and wait on the download also)?
i havent actually managed to get further than downloading a file using
but i know this wont work because i wont be able to know the filename and ill have to save it to a file in a config location
using (WebClient client = new WebClient())
{
client.DownloadFile("https://github.com/Hellzbellz123/downloadme/raw/master/TestAddon.7z", "testAddon.7z");
}
I intend to build a backend then plug it into a windows forms app for a gui because im really new to C# and programming in general
Do you mean that you don't know the filenames so you don't know how to save them locally?
If so:
//with 'url' as string
WebClient client = new WebClient();
Uri uri = new Uri(url);
client.DownloadFile(uri, uri.Segments.Last());
It takes the URL and splits it by every slash - the last item in the list is the filename..
EDIT: Improved, thanks to Jimi
That method won't work for links like "[..]/download.php?fileid="
For those links take a look at this
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).
Hello (sorry for my bad English), so basically i'm creating a simple c# application that retrieves .txt files located within the documents directory. My program is going to allow users to open these text files and edit them, then upload to my website.
the text files i'm looking to retrieve are located in this path:
Libraries -> Documents -> XYZtxtMapper -> Savedstuff -> example.txt
I'm looking for some sort of way in c# to receive the example.txt file within the path shown above, once I've retrieved the example.txt file i would call a API on my server, example:
var client = new WebClient())
client.DownloadString("https://example.com/file=" + example.txt); //<- Calls my API
Once the above is called i would use PHP to $_GET['file'], then what PHP code would i use to upload the txt file that i use $_GET['file'] to my server? Could i use fwrite() to upload the file to my server (website)?
Sorry for my bad English and me being a noob! I'm still pretty new to all this programming stuff
Your English make your explain not very well. As I understand, you want use a C# app to send a string to your server via GET method, then get it's response, right?
Then if you use WebClient:
WebClient wc = new WebClient();
string yourString = File.ReadAllText("C:\\path\\toYourFile.txt"); // Read file content
// string yourString = "C:\\path\\toYourFile.txt"; // Or you want send filename
string response = wc.DownloadString("https://example.com/pathTo/yourPHPFile.php?file=" + WebUtility.UrlEncode(yourString));
Note: You have to include pathto/yourPHPFile.php?file= to your URL, where pathto/yourPHPFile.php is your php file, ? is the begin your query. If you unfamily with this, read more about PHP GET
I am using ITextSharp for creating pdf in ASP.net, every thing works fine on my local machine, when I run it on IIS server, pdf is created successfully and can be open and view in the folder it is made, but I cannot open the pdf programmatically from C#.
I am using Process.Start(path) to open the file.
PdfWriter.GetInstance(doc, new FileStream(Server.MapPath("TransferLetter/" + filenamee), FileMode.Create));
doc.Open();
DateTime date = DateTime.Now.Date;
html = html.Replace("[Date]", Request["Date"] + "");
html = html.Replace("[Address]", Request["MailingAddress"].ToString());
html = html.Replace("[PlotNo]", Request["PlotNumber"].ToString());
html = html.Replace("[Block]", Request["Block"].ToString());
html = html.Replace("[Size]", Request["PlotSize"].ToString());
string pa = Server.MapPath("TransferLetter/" + filenamee);
System.Diagnostics.Process.Start(pa);
This will never work this way. System.Diagnostics.Process.Start() runs on the server, not the client. When you're developing on your local machine you are both the server and the client so it appears to work but once you separate these two you get your failure.
To say that again but in a different way, you are asking the server to build your PDF and then you are instructing the server to launch its local copy of Adobe Reader and display the PDF to whoever is physically logged into the server at the moment. (Ok, not 100% true but pretty close.)
Instead you need to send the PDF to the client using something like Response.Redirect() or Response.Write(). Looking at your code you should be able to perform:
Response.Redirect("TransferLetter/" + filenamee)
Using iTextSharp you don't actually have to even write the PDF to disk. You could use a MemoryStream instead of a FileStream, call MemoryStream.ToArray() write before disposing of it and using Response.Write() on that byte array. If you are writing small PDFs and/or are planning on multiple people accessing this that might be the safer way.
you need to use ~ tilde operator to specify the current project folder path
Try This:
string pa = Server.MapPath("~/TransferLetter/" + filenamee);