ASP.NET MVC - how to return a FileResult, while file is at another site?
asp.net, asp.net-mvc, c#
Solution
Use `WebClient` class to download the file from the remote url and then return it using the `Controller.File` method. `DownLoadData` method in WebClient class will do the trick for you.
So you can write an action method like this which accepts the fileName(url to the file)
public ActionResult GetImage(string fileName)
{
if (!String.IsNullOrEmpty(fileName))
{
using (WebClient wc = new WebClient())
{
var byteArr= wc.DownloadData(fileName);
return File(byteArr, "image/png");
}
}
return Content("No file name provided");
}
So you can execute this by calling
yoursitename/yourController/GetImage?fileName="http://somesite.com/logo.png
Problem
I am writing url redirector. Now I am struggling with this: Let's say I have this method: ``` public FileResult ImageRedirect(string url) ``` and I pass this string as an input: `http://someurl.com/somedirectory/someimage.someExtension`. Now, I want my method to download that image from `someurl`, and return it as a `File()`. How can I do this?