Tôi đã thực hiện cùng một hành động trong WebAPI. Tôi cần xóa tệp ngay sau khi tải xuống máy chủ biểu mẫu. Chúng tôi có thể tạo lớp thông báo phản hồi tùy chỉnh. Nó lấy đường dẫn tệp làm tham số và xóa nó sau khi nó được truyền đi.
public class FileHttpResponseMessage : HttpResponseMessage
{
private readonly string filePath;
public FileHttpResponseMessage(string filePath)
{
this.filePath = filePath;
}
protected override void Dispose(bool disposing)
{
base.Dispose(disposing);
File.Delete(filePath);
}
}
Sử dụng lớp này dưới dạng mã và sẽ xóa tệp của bạn khi tệp được ghi trên luồng phản hồi.
var response = new FileHttpResponseMessage(filePath);
response.StatusCode = HttpStatusCode.OK;
response.Content = new StreamContent(new FileStream(filePath, FileMode.Open, FileAccess.Read));
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "MyReport.pdf"
};
return response;
Nguồn
2015-05-29 06:46:34
Giải pháp phải đi trong 'câu trả lời', không phải trong câu hỏi. –