I'm quite new to using RestSharp and I've got a question that I can't find an answer to here on SO.
I've have this situation where I must download a csv-file and output the file directly in the browser. The following code illustrates how to download a file and save it to a certain path on disc.
string tempFile = Path.GetTempFileName();
using (var writer = File.OpenWrite(tempFile))
{
var client = new RestClient(baseUrl);
var request = new RestRequest("Assets/LargeFile.7z");
request.ResponseWriter = (responseStream) => responseStream.CopyTo(writer);
var response = client.DownloadData(request);
}
I want to download the csv-file and directly output the result as a download file in the browser. You know, like in Chrome the file you download will be displayed in the left bottom corner of your browser.
Can this be done using RestSharp? And if so, how? Got an example? Please share it. ;-)
Thanx!
Related
DropboxClient dbx = new DropboxClient("my_Key");
var folder = "/Apps/Images";
var file = $"fileName.jpg";
var fileToUpload = #"C:\Users\LENOVO\Test\Test\test.jpg";
using (var mem = new MemoryStream(File.ReadAllBytes(fileToUpload)))
{
var updated = await dbx.Files.UploadAsync(folder + "/" + file,
WriteMode.Overwrite.Instance,
body: mem);
Console.WriteLine("Saved {0}/{1} rev {2}", folder, file, updated.Rev);
}
i want to upload Image to Dropbox. This code is worked but i want fileToUpload to be is a web URL because images is a Web Server. i know i can download every Images step by step. But this is a loss of performance. If i write a WebUrl in the fileToUpload. i see the exception. For Example:
fileToUpload = "https:\upload.wikimedia.org\wikipedia\commons\5\51\Small_Red_Rose.JPG"
The Exception:
C:\Users\LENOVO****\bin\Debug\net6.0\https:\upload.wikimedia.org\wikipedia\commons\5\51\Small_Red_Rose.JPG
*** - is a local folder name
i want to upload image to dropbox from Web
The UploadAsync method requires that you supply the data of the file to upload directly, as you are doing in your example by retrieving the file data from the local filesystem using File.ReadAllBytes.
If you want to upload a file to Dropbox directly from a URL without downloading it to your local filesystem first, you should instead use the SaveUrlAsync method.
I have a problem downloading file from gdrive
I am using this code
DriveService service = new DriveService();
var Stream = service.HttpClient.GetStreamAsync("https://drive.google.com/open?id=blablabla");
var result = Stream.Result;
using (var fileStream = System.IO.File.Create("MyFile.exe"))
{
result.CopyTo(fileStream);
}
But I am getting size of 103kB in MyFile.exe, and it has 800 kB.
I suspect that I am not getting download url right, as I right click on the file I want to download and get shareable link in this format: https://drive.google.com/open?id=blablaid
According to www.labnol.org/internet/direct-links-for-google-drive/28356/ the Google Drive download link is https://docs.google.com/uc?export=download&id=[FILE_ID].
Otherwise you could also look at the Google Drive REST API (developers.google.com/drive/v3/web/manage-downloads).
I am working with a c# web api controller, where my code is supposed to create several word documents and zip them into a zip file. I am using Ionic.Zip to zip the file and return it to the code below as the input parameter.
The code is supposed to push the zip file to the user to download, but it is loading the zip file in a window instead.
I am using the following code:
protected HttpResponseMessage ZipContentResult(ZipFile zipFile)
{
// inspired from http://stackoverflow.com/a/16171977/92756
var pushStreamContent = new PushStreamContent((stream, content, context) =>
{
zipFile.Save(stream);
stream.Close(); // After save we close the stream to signal that we are done writing.
}, "application/zip");
return new HttpResponseMessage(HttpStatusCode.OK) {Content = pushStreamContent};
}
But when I run it, the zip file loads into the window instead of prompting to save the file.
this is what I get when I run it:
PKxS�I$$filedocumentname.docx
�0�L�G#.�L�G#.�L���UP#�-JB��ew.
�\��u��$8��;0�\w�����Su�s��y��z��U�]�ת���*�3r,�L6zy4�O00>`a`�a�xj���i��i���f�a��������S��O1���F��pQ�x�R��
�W�ȟ<vJS�8ZTGCZ�Y/....
any ideas of what I am doing wrong or how to fix this?
much appreciated.
I'm trying to get a link from a pastebin. Where the link is the only text in the raw paste. Then I want to download a file from the link in pastebin.
WebRequest request = WebRequest.Create("http://pastebin.com/raw/Dtdf2qMp");
WebResponse response = request.GetResponse();
System.IO.StreamReader reader = new
System.IO.StreamReader(response.GetResponseStream());
Console.WriteLine(reader.ReadToEnd());
WebClient client = new WebClient();
client.DownloadFile (Link gotten from pastebin here, "c:\\File");
System.Threading.Thread.Sleep(5000);
Instead of dumping the text read to console output, you should assign it to a variable.
var pastebinOutput = reader.ReadToEnd();
Then just pass that as the link for the DownloadFile method. If you want to do verification that it's actually a URL you got from the original pastebin, you can look into System.Uri's TryCreate method.
I've got a solution - assuming you have your link in the raw pastebin link (mine is a .txt file saying 'it worked') I suggest you copy and paste the code below exactly - if you get an file saying 'it worked' then you can change the pastebin link & file names. If you don't want to open the file then remove Process.Start - if you want to change the delay just change the number (it's in milliseconds) Also, you can change the format from .txt to .exe or whatever your file is (or you can remove it so its the defualt name in the download link):
WebRequest request = WebRequest.Create("https://pastebin.com/raw/QAWufg1z");
WebResponse response = request.GetResponse();
System.IO.StreamReader reader = new
System.IO.StreamReader(response.GetResponseStream());
var pastebinOutput = reader.ReadToEnd();
WebClient client = new WebClient();
client.DownloadFile(pastebinOutput, #".\downloaded.txt");
MessageBox.Show("File should open automatically in the next minute. Please wait...");
await Task.Delay(3000); //3000 = 3 seconds
Process.Start(#".\downloaded.txt");
I am using WebClient.DownloadFile to download a small executable file from the internet. This method is working very well. However, I would now like to download this executable file into a byte array rather than onto my hard drive. I did some reading and came across the WebClient.DownloadData method. The problem that I am having with the downloadData method is that rather than downloading my file, my code is downloading the HTML data behind my file's download page.
I have tried using dozens of sites - each brings me the same issue. Below is the code I am using.
// Create a new instance of the System.Net 'WebClient'
System.Net.WebClient client = new System.Net.WebClient();
// Download URL
Uri uri = new Uri("http://www35.multiupload.com:81/files/4D7B4D2BFC3F1A9F765A433BA32ED2C5883D0CE133154A0FDB7E7786547A3165DA62393141C4AF8FF36C75222566CF3EB64AF6FBCFC02099BB209C891529CF7B90C83D9C63D39D989CBB8ECE6DE2B83B/Project1.exe");
byte[] dbytes = client.DownloadData(uri);
MessageBox.Show(dbytes.Length.ToString()); // Not the size of my file
Keep in mind that I am attempting to download the data of an executable file into a byte array.
Thank you for any help,
Evan
You are attempting to download a file using an expired token url. See below:
URL: http://www35.multiupload.com:81/files/4D7B4D2BFC3F1A9F765A433BA32ED2C5883D0CE133154A0FDB7E7786547A3165DA62393141C4AF8FF36C75222566CF3EB64AF6FBCFC02099BB209C891529CF7B90C83D9C63D39D989CBB8ECE6DE2B83B/Project1.exe`
Server: www35
Token:
4D7B4D2BFC3F1A9F765A433BA32ED2C5883D0CE133154A0FDB7E7786547A3165DA62393141C4AF8FF36C75222566CF3EB64AF6FBCFC02099BB209C891529CF7B90C83D9C63D39D989CBB8ECE6DE2B83B
You can't just download a file by waiting for the timer to end, and copy the direct link, it's a "token" link. It will only work for a specified period of time before redirecting you back to the download page (which is why you are getting HTML instead of binary data).
Workaround
You will have to download the multiupload's HTML and parse the direct download link from the HTML source code. Only this way provides a sure-fire way of getting an up-to-date token url.
How #Dark Slipstream said, you're attempting to download a file using an expired token url
look how get the new url:
System.Net.WebClient client = new System.Net.WebClient();
// Download URL
Uri uri = new Uri("http://www.multiupload.com/39QMACX7XS");
byte[] dbytes = client.DownloadData(uri);
string responseStr = System.Text.Encoding.ASCII.GetString(dbytes);
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(responseStr);
string urlToDownload = doc.DocumentNode.SelectNodes("//a[contains(#href,'files/')]")[0].Attributes["href"].Value;
byte[] data = client.DownloadData(uri);
length = data.Length;
I dont parsing the exceptions