在我的 ApiController 类中,我有以下方法来下载由服务器创建的文件。
public HttpResponseMessage Get(int id)
{
try
{
string dir = HttpContext.Current.Server.MapPath("~"); //location of the template file
Stream file = new MemoryStream();
Stream result = _service.GetMyForm(id, dir, file);
if (result == null)
{
return Request.CreateResponse(HttpStatusCode.NotFound);
}
result.Position = 0;
HttpResponseMessage response = new HttpResponseMessage();
response.StatusCode = HttpStatusCode.OK;
response.Content = new StreamContent(result);
return response;
}
catch (IOException)
{
return Request.CreateResponse(HttpStatusCode.InternalServerError);
}
}
除了默认的下载文件名是它的 id 之外,一切工作都很完美,因此用户可能每次都必须在另存为对话框时键入他/她自己的文件名。有没有办法在上面的代码中设置默认文件名?