Download file with File Address c# - c#

Here I download a file from website which works good. But I wanted to download a Folder with contents.
Before Download Example
MyprojectApp/Test.exe
My Code
WebClient wc = new WebClient();
String Filename = "some.txt";
Uri uri = new Uri("http://127.0.0.1/New/" + Filename);
wc.DownloadFileAsync(uri, "some1.txt");
After Download Example(What I need)
MyprojectApp/Test.exe
MyprojectApp/New/some.txt

Related

Download Zip file with Webclient c# with POST method

I have build an API which gives output in zip file when more than one file is requested
Now I have to download this zip file in C# WPF app.
To access API, we have to use POST ( instead of GET ) and JSON parameters.
I am able to download one file as string with help of below code
WebClient client = new WebClient();
var vm = new { from = "A", to = "S", files= "all", type = "csv", file = "Single" };
client.Headers[HttpRequestHeader.ContentType] = "application/json";
var dataString = JsonConvert.SerializeObject(vm);
var response = client.UploadData("https://myurl.com/data", "POST", System.Text.Encoding.ASCII.GetBytes(dataString));
But not able to figure out how to download zip file and save it to disk

Downloading File in C# with DownloadUrl [duplicate]

I have a web service, like this example for downloading a zip file from the server. When i open the URL through web browsers,I can download the zip file correctly. The problem is when I try to download the zip file through my desktop application. I use the following code to download:
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += new AsyncCompletedEventHandler(Completed);
webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
webClient.DownloadFileAsync(new Uri(#"http://localhost:9000/api/file/GetFile?filename=myPackage.zip"), #"myPackage.zip");
After testing this, I get the myPackage.zip downloaded, but it is empty, 0kb. Any help about this or any other server code + client code example?
You can try to use HttpClient instead. Usually, it is more convenient.
var client = new HttpClient();
var response = await client.GetAsync(#"http://localhost:9000/api/file/GetFile?filename=myPackage.zip");
using (var stream = await response.Content.ReadAsStreamAsync())
{
var fileInfo = new FileInfo("myPackage.zip");
using (var fileStream = fileInfo.OpenWrite())
{
await stream.CopyToAsync(fileStream);
}
}

Delay before starting download of pdf file using DownloadFile C#

I am attempting to download a large 25 megabyte pdf file using the following code:
string url = "http://aaa.aaa/test.pdf";
string clientfile = HttpContext.Current.Server.MapPath("~/123.pdf");
WebClient wc = new WebClient();
wc.DownloadFile(new Uri(url, UriKind.Absolute), clientfile);
However the file signature is corrupted and does not download correctly. Is there a way to delay the file download before the download actually starts?
I know the file is correct since if I download it from a browser, the file is not corrupted.
Thanks
I found the problem. When downloading a pdf file, it is actually zipped in a format called "GZIP". When using a browser, the file is automatically unzipped for the client. When using the WebClient DownloadFile method in .NET, it must be done manually. First create a class to derive the WebClient class as follows:
public class GzipWebClient : WebClient
{
protected override WebRequest GetWebRequest(Uri address)
{
HttpWebRequest request = base.GetWebRequest(address) as HttpWebRequest;
request.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip;
return request;
}
}
Then change your code to use the above class:
string url = "http://aaa.aaa/test.pdf";
string clientfile = HttpContext.Current.Server.MapPath("~/123.pdf");
GzipWebClient wc = new GzipWebClient();
wc.DownloadFile(new Uri(url, UriKind.Absolute), clientfile);
The pdf file will now be correctly downloaded and unzipped automatically.

Download image from "no-hotlink" url

I try to download image on stooorage.com using following code
Webclient wc = new Webclient();
wc.DownloadFile(Link,Filepath);
But this is no-hotlink site ?
Link to test:
http://www.stooorage.com/show/3105/11956084_ngoc_han.jpg
Need a trick :(

How to save Image Control Image in Folder in Asp.net

I know how to save an image to a folder using the fileupload control with the saveas method. But I to take an image from the image control and save it to a file without using the fileupload control n save it in folder.
string filepath = img1.ImageUrl;
using (WebClient client = new WebClient())
{
client.DownloadFile(filepath,Server.MapPath("~/Image/apple.jpg"));
}
Do you know image path? you can get image path from image control and then download image in code:
Download image from the site in .NET/C#
using(WebClient client = new WebClient())
{
client.DownloadFile("http://www.example.com/image.jpg", localFilename);
}
First Get the Url of Image and then using webclient you can save file in folder
string filepath = img1.ImageUrl;
using (WebClient client = new WebClient())
{
client.DownloadFile(filepath,Server.MapPath("~/Image/apple.jpg"));
}
This will save image in Image Folder with ImageName apple...

Categories

Resources