While owning/hosting a Web API 2 application, I am writing a desktop application where I need to download a .zip file based on certain criteria. I have come across two approaches and am not sure which is more secure and proper:
1) In the initial approach, I made a function call to my web api with the provided criteria. The API would create the zip file and return a specific path string. Then, I used the WebClient.DownloadFileAsync
to download the related file from the path:
private async Task<bool> DownloadFile(SOME PARAMETER)
{
var result = false;
var _stopwatch = new Stopwatch();
try
{
using (var _client = new WebClient())
{
// Returns the Uri of the file
var downloadAddress = await GetDownloadFileAddress(SOME PARAMETER);
if (!string.IsNullOrEmpty(downloadAddress))
{
// Some local path
var downloadFilePath = "C:\MyLocalPath"
_stopwatch.Start();
_client.DownloadFileCompleted += wc_DownloadFileCompleted;
_client.DownloadProgressChanged += wc_DownloadProgressChanged;
_client.DownloadFileAsync(new Uri(downloadAddress), downloadFilePath);
result = true;
}
}
}
catch (Exception ex)
{
ModErrorHandler.HandleError(ex, MethodBase.GetCurrentMethod().Name, Application.ProductName);
}
return result;
}
I like the above because the files could be near a 1 gig and so I can display progress bar to show the download progress and also copy the file into a specific local path. However, I am concerned that it is not a secure way to pass the Uri openly(?!)
2) Then, I thought I could return the related file from the API directly. A sample code could be found here: How to return a file (FileContentResult) in ASP.NET WebAPI
The api authentication will now take care of security and invalid download requests. However, I am not sure if this is a wise approach for files that over 1gb. More importantly, I am not sure how to show the download progress in my progress bar and how to copy the file into a specific local path, both of which would be taken care of in the first approach.
Can someone advise which method is truly the correct way? And if 2, suggest a few hints for the questions that I have raised after?