假設您使用HttpClient
類來提出請求,您可以使用HttpResponseMessage
,查詢返回以決定是否要下載該文件。例如:
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync("http://MySite/my/download/path").ConfigureAwait(false);
if (!String.Equals(response.Content.Headers.ContentDisposition.DispositionType, "attachment", StringComparison.OrdinalIgnoreCase)) {
return;
}
// Call some method that will check if the file extension and/or media type
// of the file are acceptable.
if (!IsAllowedDownload(response.Content.Headers.ContentDisposition.FileName, response.Content.Headers.ContentType.MediaType)) {
return;
}
// Call some method that will take a stream containing the response payload
// and write it somewhere.
WriteResponseStream(await response.Content.ReadAsStreamAsync().ConfigureAwait(false));
我不認爲一個服務器甚至會允許你下載一個文件,從文件名/完整路徑的文件。 – Austinh100
你應該在響應頭中有文件名'Content-Disposition' – py3r3str
沒有擴展名是未知的MIME類型。如果IIS沒有設置爲此服務,您將無法下載它... – Tim