A few comments -
Don't open the file as read/write, read is plenty good.
The second route is fine you don't need the first one, you can alternatively just post to the following route:
api/{controller}/{FileName}/{CEQRNumber}/{LatestMileStone}
and remove the [ActionName]
attribute from the GetFiles.
Even simpler you can use the following route
api/{controller}
and pass the parameters as a query string.
so it's going to look like this
config.Routes.MapHttpRoute(
routeTemplate: "api/{controller}"
);
Your url is going to look like:
http://xxx/api/Files?FileName=foo&CEQRNumber=bar,&LatestMileStone
The controller code:
public class FilesController : ApiController
{
public HttpResponseMessage GetFile(string FileName, string CEQRNumber, string LatestMileStone)
{
var file = GetListofFilesByCEQRAndMilestone(FileName, CEQRNumber, LatestMileStone);
var path = file.FilePath;
var extension = file.FileExtention;
var result = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new FileStream(path, FileMode.Open,FileAccess.Read);
result.Content = new StreamContent(stream);
// do the header work
return result;
}
}
For your client code instead of ReadAsStringAsync use
using (Stream responseStream = await response.Content.ReadAsStreamAsync())
{
// Read response stream
await ReadResponseStreamAsync(responseStream, fileName);
}
and don't forget to validate the response code on the client
For your comment on how to save a file from a stream:
private void ReadResponseStreamAsync(stream responseStream, string fileName)
{
using (var fileStream = File.Create("C:\\Path\\To\\" + fileName))
{
responseStream.CopyTo(fileStream);
}
}