I am downloading a file off a website and it always saves into my Download files, is there a way where i can select where to save the file to?
public void myDownloadfile(string token, string fileid, string platform)
{
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("Token", token);
parameters.Add("fileid", fileid);
parameters.Add("plateform", platform);
string url;
url = "https://formbase.formmobi.com/dvuapi/downloadfile.aspx?" + "token=" + token + "&fileid=" + fileid + "&platform=" + platform;
System.Diagnostics.Process.Start(url);
}
System.Diagnostics.Process.Start just opens your default web browsers on the required url.
You can set your browser to open save as dialog.
But better use WebClient.DownloadFile: http://msdn.microsoft.com/en-us/library/ez801hhe.aspx
It receives the path of the target file as one of it's parameters.
Since you are downloading the file using your system's standard browser, you have to change the setting there.
Otherwise, you probably want to use the WebClient class to download a file, since it is as easy to use as
using System.Net;
WebClient webClient = new WebClient();
webClient.DownloadFile("http://mysite.com/myfile.txt", #"c:\myfile.txt");
(example from here)
Don't use Process.Start that will launch the default browser to download the file and the download location will be very dependent on the users system settings. Use WebClient to download it instead and it will be easier to specify a location.
public void myDownloadfile(string token, string fileid, string platform)
{
Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("Token", token);
parameters.Add("fileid", fileid);
parameters.Add("plateform", platform);
string url;
url = "https://formbase.formmobi.com/dvuapi/downloadfile.aspx?" + "token=" + token + "&fileid=" + fileid + "&platform=" + platform;
System.Net.WebClient wc = new System.Net.WebClient()
wc.DownloadFile(url, "C:\\myFile.ext")
}
You can use WebClient to download the data and use SaveFile Dialog to set the default location where you which to start.
http://www.techrepublic.com/blog/programming-and-development/download-files-over-the-web-with-nets-webclient-class/695
You can use HttpResponse.
Go through the link: prompt a save dialog box to download a file
Related
I need any player's client to be able to upload and download files (they are text files that have replay data. I'm making a racing game and want players to be able to send and download replays.) while in-game. I'm trying to use Dropbox but I'm having problems. I'm open to other file hosting services but I don't really know how to send and get data from them. I keep making little changes here and there and they all give me different errors. Is there anything I'm doing blatantly wrong here? I'm expecting the main problem to be in the dropbox URL I'm using. Here's what my code looks like:
private void DownloadReplay()
{
string l_playerID = PlayFabManager.load_PlayfabId;
string l_levelID = PlayFabManager.load_levelID;
using (WebClient wc = new WebClient())
{
string path = Path.Combine(Application.persistentDataPath, $"{l_playerID}{l_levelID}.replay");
string url = ("https://www.dropbox.com/s/f786vep0z9yd11s/"+ $"{l_playerID}{l_levelID}");
wc.Credentials = new NetworkCredential("xx", "xx");
wc.DownloadFile((new System.Uri(url)), path);
}
}
private void UploadReplay()
{
string s_playerID = PlayFabManager.load_PlayfabId;
string s_levelID = PlayFabManager.load_levelID;
using (WebClient client = new WebClient())
{
string path = Path.Combine(Application.persistentDataPath, $"{s_playerID}{s_levelID}.replay");
string url = Path.Combine("https://www.dropbox.com/s/f786vep0z9yd11s/" + $"{s_playerID}{s_levelID}");
client.Credentials = new NetworkCredential("xx", "xx");
client.UploadFile(url, path);
}
}
Right now this one gives me an error 404, even though the file matching the string is in my dropbox.
We have been requested to go and Download an order file from our customers site via a url.
I want to do something like this.
string remoteUri = "http://www.contoso.com/library/homepage/images/";
string fileName = "ms-banner.gif", myStringWebResource = null;
// Create a new WebClient instance.
using (WebClient myWebClient = new WebClient())
{
myStringWebResource = remoteUri + fileName;
// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(myStringWebResource, fileName);
}
But the URL will be variable as we have to specify the Date and Time within the URL we post.
And the File we download will be variable also.
As I'm new to C# I would like some advise as to how to achieve this?
Thanks In Advance
It depends on how the URLs will be generated. Do they follow a pattern? Do you know them in advance?
private void GetVariableFile(string remoteUri, string filename) {
string myStringWebResource = null;
// Create a new WebClient instance.
using (WebClient myWebClient = new WebClient()) {
myStringWebResource = remoteUri + fileName;
// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(myStringWebResource, fileName);
//Do stuffs
}
}
You might wanna pass the Uri & the file obtained to a method which will handle the download and the elaboration, or in case you need to return the result, change the return type so that it can return the info you need.
I've been using xamarin for a while and the current project I'm working on will require some mp3 files to be downloaded.
I saw tutorials for downloading a file and downloading an image, but they didn't lead me anywhere and are for iOS.
Given a url www.xyz.com/music.mp3, how do I download the mp3 file and save it?
Simplest way is to use WebClient and if on the UI thread then call method DownloadFileTaskAsync:
button.Click += async delegate
{
var destination = Path.Combine(
System.Environment.GetFolderPath(
System.Environment.SpecialFolder.ApplicationData),
"music.mp3");
await new WebClient().DownloadFileTaskAsync(
new Uri("http://www.xyz.com/music.mp3"),
destination);
};
Xamarin.iOS Docs converted to download bytes
The Xamarin.iOS docs WebClient sample for downloading a file should work just fine after you tweak from downloading a string to downloading bytes (note DownloadDataAsync and DownloadDataCompleted vs String sibling functions).
var webClient = new WebClient();
webClient.DownloadDataCompleted += (s, e) => {
var text = e.Result; // get the downloaded text
string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string localFilename = "downloaded.mp3";
string localPath = Path.Combine (documentsPath, localFilename);
File.WriteAllText (localpath, text); // writes to local storage
};
var url = new Uri("http://url.to.some/file.mp3"); // give this an actual URI to an MP3
webClient.DownloadDataAsync(url);
Using HttpClient
If you want to use the newer HttpClient library. Add a reference to System.Net.Http to your Xamarin.Android project and give something like this a shot.
var url = new Uri("http://url.to.some/file.mp3");
var httpClient = new HttpClient ();
httpClient.GetByteArrayAsync(url).ContinueWith(data => {
string documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string localFilename = "downloaded.mp3";
string localPath = Path.Combine (documentsPath, localFilename);
File.WriteAllBytes (localPath, data.Result);
});
I have link of a PDF file located on a website e.g. (https://www.mysite.com/file.pdf).
What I need is prompt the user (on Client side) the SaveAs box to choose the file location to save the file.
I tried the SaveFileDialog , but got know that it is only for Windows Forms. and my application is web based.
C# Code
var fileNumber = lblFileNumber.Text;
string fileDownloadLink = "http://www.mysite.com/" + fileNumber + ".pdf";
string fileName = fileNumber + ".pdf";
bool exist = false;
try
{
var request = (HttpWebRequest)System.Net.WebRequest.Create(fileDownloadLink);
using (var response = (HttpWebResponse)request.GetResponse())
{
exist = response.StatusCode == HttpStatusCode.OK;
}
}
catch
{
}
if (exist)
{
var wClient = new WebClient();
wClient.DownloadFile(fileDownloadLink, fileName);
}
I wrote the above code, it does check if the file is exist on the file location, but
how to prompt user to choose the location to where he want to save file and save on his local hard drive?
It is not possible to do that. When the user wants to download a file, the browser will decide how to show it to the user.
Write a file from disk directly:
HttpContext.Current.Response.TransmitFile(#"C:\yourfile.pdf");
Or from another source, loaded as a byte array:
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.BinaryWrite(bytesOfYourFile);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
I had shared some documents to public on web. Is there any way to use Google API to retrieve these documents without login?
What you would do is
// http://msdn.microsoft.com/en-us/library/system.net.webclient.aspx
WebClient wc = new WebClient();
// http://msdn.microsoft.com/en-us/library/ms144200.aspx
string priceList=wc.DownloadString(
new Uri(
"https://docs.google.com/spreadsheet/pub?hl=en_US&hl=en_US&key=0Aic3jYFcLTrldEhQU1dDTzdTdFBhaVA4OGtPX2FUMUE&single=true&gid=0&output=txt");
// http://msdn.microsoft.com/en-us/library/3cc9y48w.aspx
wc.Dispose(); // Free resources
// http://msdn.microsoft.com/en-us/library/tabh47cf.aspx
string[] lines = priceList.Split(
new string[] {
"\r\n",
"\n\r", // Rarely ever used, but be prepared
"\n" // Match this and
"\r" }, // this last
StringSplitOptions.None);
Here is the way I do it for my document:
using (WebClient wc = new WebClient()) {
string priceList=wc.DownloadString("https://docs.google.com/spreadsheet/pub?hl=en_US&hl=en_US&key=0Aic3jYFcLTrldEhQU1dDTzdTdFBhaVA4OGtPX2FUMUE&single=true&gid=0&output=txt");
string[] lines = priceList.Split('\n');
}
This will get a Spreadsheet in a text format to the variable priceList.
If you have shared document with public access you can share its download URL to download it without login into google. To build download URL through google api you need to have resource Id for that particular document.
For more information have a look at this : Google Docs : Download Documents and files