How do I take an image from an url and save it locally?
http://someurl.com/imagename.jpg
-> c:\apppath\imgfolder
ASP.NET C#
I am not very good with IO stuff, hope someone can help me out :)
Try this.
System.Net.WebClient wc = new System.Net.WebClient();
wc.DownloadFile("http://www.domin.com/picture.jpg", #"C:\Temp\picture.jpg");
Since you've tagged this with ASP.NET, it's worth pointing out that if you put this code in your ASP.NET Website/Web Application, you'll need to make sure the the Identity/Account that your application is running as, has permission to write the file to the file system.
Related
I have been tasked with finding a way to save files given a link to someones amazon bucket. These files are attachments uploaded to a ticketing system, and when i used their API to pull the ticket out, i am given a link like the one below(I removed all the private stuff and replaced with XXXXX)
https://s3.amazonaws.com/cdn.XXXXXXX.com/data/helpdesk/attachments/production/36012348362/original/1.jpeg?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=XXXXXXXXXXXXXXXXXFus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20180808T205357Z&X-Amz-Expires=86400&X-Amz-Signature=XXXXXXXXXXXXXX-Amz-SignedHeaders=Host
As you can see, the links time out after 1 day. I am connecting 2 API's together, and i need to be able to upload an actual file into the second API instead of just placing a link that times out after a day. I have tried to use Webclient downloadfileAsync, but all i get is a blank file output. I can go to the link in my browser and do a right click, save image as, so i know its possible, but i havent had any luck programatically. Below is the code snipped i am currently using to try and download it.
using (WebClient wc = new WebClient())
{
wc.DownloadFileAsync(new System.Uri(url), "image.jpg");
}
Any insight or help would be awesome.
Thanks in advance!
I was able to get this working by not using the async version of DownloadFile. Thanks to all the people who helped me resolve this.
i just wanna upload afile from a fixed path, so i dont want browse button, I need just a TextBox(Path of my file some thing like c:/junk/upload) and and upload button.some thing like
[TextBox.Path][UploadButton]
or can i get the code in java applets or any other lanaguage?
For security reasons, a webpage loaded in a browser from internet cannot directly refer the local file system. You need a desktop application or plugin to achieve this.
no we can make using asp.net or java applets and yes what you said is right, security reasons! , but every applications has a sand box limits and thats include file handling as default , and we achived that. with out problem
Is there any way to take a screenshot with ASP.net C#, or can we take screenshot of any URL?
Asp.net runs on the web server so there is not way to take a screen shot of the client.
you should use java script and perhaps an ActiveX control to achieve that.
I would not recommend you to go down this road anyway
what you can it to store the content of the page (if this can help) doing something like the below
WebClient client = new WebClient();
string downloadString = client.DownloadString("http://www.gooogle.com");
You can then create and .html file which will look like that page
,Hope i get this clear.
I have a asp.net website, as part of it i generate a bitmap and need to save it some were so i can display it later on.
for now my sites is locate here:
http://tools.myDomain.com/mySite/
I have created a directory with in my site:
http://tools.myDomain.com/mySite/img/
Now i am trying to save my bitmap to there like this:
bitmap.Save(#"http://tools.myDomain.com/mySite/img/bitmap.png");
I can get this to work, not from my computer locally and not when i do Publish to my site...
Any ideas how can i fix this problem?
Thank you
You need to save it on the server not on the web
bitmap.Save(HttpContext.Current.Server.MapPath("/img/bitmap.png"));
try
bitmap.Save(Server.MapPath("/mySite/img/bitmap.png"));
Or you can use an absolute path
bitmap.Save("c:\inetpub\wwwroot\mySite\img\bitmap.png");
Note that the path is just an example and should be your absolute path.
You need to add permissions for ASPNET user. It will work for sure.
I have a C# client which once every hour needs to post some zip files to ASP.Net site. This needs to be completely automated with no user interaction.
Wondering the best way to go about it.
Ideally would like to post the file without setting up any non .aspx / .asp pages.
Thanks for the help!
It depends on what the target site expects as content type. If it is multipart/form-data then a simple WebClient should do the job:
using (var client = new WebClient())
{
byte[] result = client.UploadFile(
"http://foo.com/index.aspx", #"d:\foo\bar.zip"
);
// TODO: Handle the server response if necessary
}
Send a HttpRequest containing all the necessary information including the bytes of the file. Google should help you on this one.
Nevertheless, I don't understand why you don't want to use a non .aspx page for this. A generic handle (.ashx) is suitable for this. But I still suggest you use another way to upload that file, e.g. per FTP and use a service that watches the directoy with a FileWatcher to determine and act on changes
In order to automate the task, you can use a DispatcherTimer (http://msdn.microsoft.com/en-us/library/system.windows.threading.dispatchertimer.aspx), assigning a handler to the Tick event.