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...
Related
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
I am using "webclient" to download and save a file by url in windows application.
here is my code:
WebClient wc = new WebClient();
wc.Headers.Add(HttpRequestHeader.Cookie, cc);
wc.DownloadFile(new Uri(e.Url.ToString()), targetPath);
this is working fine local system.(downloading the file and saved to target path automatically with out showing any popup).
But when i am trying to execute the .exe in server its showing save/open popup.
Is there any modifications require to download a file in server settings.
Please help me to download the file with out showing popup in server too.
thanks in advance..
Finally i got the solution for this issue..
herw the code:
WebClient wc = new WebClient();
wc.Headers.Add(HttpRequestHeader.Cookie, cc);
using (Stream data = wc.OpenRead(new Uri(e.Url.ToString())))
{
using (Stream targetfile = File.Create(targetPath))
{
data.CopyTo(targetfile);
}
}
here i just replaced the code
wc.DownloadFile(new Uri(e.Url.ToString()), targetPath);
with the blow lines:
using (Stream data = wc.OpenRead(new Uri(e.Url.ToString())))
{
using (Stream targetfile = File.Create(targetPath))
{
data.CopyTo(targetfile);
}
}
Now its working fine..
Thanks all for ur response..
I downloaded a image by using this method .
public Image getImageFromURL(String sURL)
{
using (WebClient wc = new WebClient())
{
var data = wc.DownloadData(sURL);
var image = Image.FromStream(new MemoryStream(data));
return image;
}
}
In my razor view and it returns me Bitmap object , but when i want to show the downloaded in image then it doesn't work
Thanks in advance
If you get Image in response from your getImageFromURL(String sURL) method.
you need to store image at any location in your application, then you can render it on view.
you can use this code.
var image = Image.FromStream(new MemoryStream(data));
string path=Path.Combine(Server.MapPath("~/images"), imagename);
image.Save(path);
this will save image at your images folder, and then you can render your image from this images folder location on view.
<img src="~/images/imagename" alt="" />
I tryed to do a webbrowser that fro, him i get the text.
But insted of getting the text is downloading to my computer the file.
How can i get this text without using it?
Thanks
You can use a WebClient:
string output = string.Empty;
using (WebClient wc = new WebClient())
{
output = wc.DownloadString("http://stackoverflow.com");
}
I have url that point to image for example :
http://cg009.k12.sd.us/images/smilefacemoving.gif
instead of user will insert file i want to bind this file(image) to the c# fileupload control .
so the fileupload object will hold this file(image).
Just have a Textbox on your page to let them enter a URL and then do this when the form is submitted...
string url = YOUR_TEXTBOX.Text();
Uri uri;
try {
uri = new Uri(url);
}
catch (UriFormatException ex) {
// Error
throw ex;
}
byte[] file;
using (System.Net.WebClient client = new System.Net.WebClient()) {
file = client.DownloadData(uri);
}
// now you have the file
Watch out for uploaded viruses :)
I'm honestly not sure what you're asking. Are you asking how you can upload a file referenced via a URL to your own server utilizing the fileupload control?