This is the link that i study to implement the download file from web api, i tried download file with these URL
http://localhost:49932/api/simplefiles/1.zip <--NOT WORKING, complain method not found http://localhost:49932/api/simplefiles/1 <--able to call the action name, but why?
i know something to do with the ".zip" extension putting in the URL that cause failure but i'm just not get it and not sure what's wrong, could anyone explain a little?
Interface
public interface IFileProvider
{
bool Exists(string name);
FileStream Open(string name);
long GetLength(string name);
}
Controller
public class SimpleFilesController : ApiController
{
public IFileProvider FileProvider { get; set; }
public SimpleFilesController()
{
FileProvider = new FileProvider();
}
public HttpResponseMessage Get(string fileName)
{
if (!FileProvider.Exists(fileName))
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
FileStream fileStream = FileProvider.Open(fileName);
var response = new HttpResponseMessage();
response.Content = new StreamContent(fileStream);
response.Content.Headers.ContentDisposition
= new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = fileName;
response.Content.Headers.ContentType
= new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.ContentLength
= FileProvider.GetLength(fileName);
return response;
}
}
WebAPIConfig
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{filename}",
defaults: new { id = RouteParameter.Optional }
);