C# 使用WebAPI2下载文件
我正在尝试使用WebAPI2下载一个文件。但是当我在浏览器中直接给出url时,浏览器给出了“网页不可用” 我已经写了以下自定义操作C# 使用WebAPI2下载文件,c#,asp.net-web-api,asp.net-web-api2,C#,Asp.net Web Api,Asp.net Web Api2,我正在尝试使用WebAPI2下载一个文件。但是当我在浏览器中直接给出url时,浏览器给出了“网页不可用” 我已经写了以下自定义操作 public class FileActionResult : IHttpActionResult { public FileActionResult(string data) { this.Data = data; } public string Data { ge
public class FileActionResult : IHttpActionResult
{
public FileActionResult(string data)
{
this.Data = data;
}
public string Data { get; private set; }
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
string tempFolderPath = HttpContext.Current.Server.MapPath("~/api/tempfiles");
HttpResponseMessage response = new HttpResponseMessage();
Guid guid = Guid.NewGuid();
string folderPath = Path.Combine(tempFolderPath, guid.ToString());
if(!Directory.Exists(folderPath))
{
Directory.CreateDirectory(folderPath);
}
string filePath = Path.Combine(folderPath, guid.ToString() + ".json");
File.WriteAllText(filePath, Data);
string zipFile = folderPath + ".zip";
ZipFile.CreateFromDirectory(folderPath, zipFile);
response.Content = new StreamContent(File.OpenRead(zipFile));
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "file.zip",
DispositionType = "attachment"
};
return Task.FromResult(response);
}
}
公共类文件ActionResult:IHttpActionResult
{
公共文件ActionResult(字符串数据)
{
这个。数据=数据;
}
公共字符串数据{get;private set;}
公共任务执行同步(CancellationToken CancellationToken)
{
字符串tempFolderPath=HttpContext.Current.Server.MapPath(“~/api/tempfiles”);
HttpResponseMessage response=新的HttpResponseMessage();
Guid=Guid.NewGuid();
字符串folderPath=Path.Combine(tempFolderPath,guid.ToString());
如果(!Directory.Exists(folderPath))
{
CreateDirectory(folderPath);
}
字符串filePath=Path.Combine(folderPath,guid.ToString()+“.json”);
writealText(文件路径,数据);
字符串zipFile=folderPath+“.zip”;
CreateFromDirectory(folderPath,ZipFile);
response.Content=newstreamcontent(File.OpenRead(zipFile));
response.Content.Headers.ContentType=新的MediaTypeHeaderValue(“应用程序/八位字节流”);
response.Content.Headers.ContentDisposition=新的ContentDispositionHeaderValue(“附件”)
{
FileName=“file.zip”,
DispositionType=“附件”
};
返回Task.FromResult(响应);
}
}
我不确定这里出了什么问题。有人能帮我吗
编辑
当我使用谷歌邮递员检查方法时,它显示了正确的响应。如何强制浏览器下载文件?
谢谢代码工作正常。然而,该问题与Telerik控制有关。我在我的网站上使用Telerik控件。Telerik在下载文件时进行了一些压缩。 我不得不绕过我的url使它工作
<sectionGroup name="telerik.web.ui">
<section name="radCompression" type="Telerik.Web.UI.RadCompressionConfigurationSection, Telerik.Web.UI" allowDefinition="MachineToApplication" requirePermission="false"/>
</sectionGroup>
</configSections>
<telerik.web.ui>
<radCompression>
<excludeHandlers>
<add handlerPath="some path" matchExact="false"/>
</excludeHandlers>
</radCompression>
</telerik.web.ui>
我觉得这个代码不错。您的WebAPI方法可能有问题吗?你确定你在那里使用了正确的HTTP动词吗?我的意思是“页面不可用”可能导致错误的方法调用,例如,你的方法需要GET,但你使用了POST,或者方法参数的数量错误,或者自定义路由问题。尝试使用F12浏览器工具捕捉您的呼叫,并分析输入参数和服务器响应。我将通过点击url调试该方法