Asp.net 使用webService下载文件
为了从服务器下载文件,我正在使用上述web服务代码。它在本地计算机上运行良好,但当我在服务器上托管web服务并尝试使用该服务时,会出现以下错误Asp.net 使用webService下载文件,asp.net,web-services,Asp.net,Web Services,为了从服务器下载文件,我正在使用上述web服务代码。它在本地计算机上运行良好,但当我在服务器上托管web服务并尝试使用该服务时,会出现以下错误 [WebMethod()] public void GetFileByDocumentNumber(string DocumentNumber) { string FilePath = GetFile(DocumentNumber); string FullPath = ConfigurationManag
[WebMethod()]
public void GetFileByDocumentNumber(string DocumentNumber)
{
string FilePath = GetFile(DocumentNumber);
string FullPath = ConfigurationManager.AppSettings["FilePath"] + FilePath;
DownloadToBrowser(FullPath);
}
private void DownloadToBrowser(string filePath)
{
FileInfo file = new FileInfo(filePath);
Context.Response.Clear();
Context.Response.ClearHeaders();
Context.Response.ClearContent();
Context.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Context.Response.AddHeader("Content-Length", file.Length.ToString());
Context.Response.ContentType = "text/plain";
Context.Response.Flush();
Context.Response.TransmitFile(file.FullName);
Context.Response.End();
}
此错误的原因是什么?尝试用
应用程序/octet流
替换内容类型
您应该通过WebMethod
返回文件内容
Client found response content type of 'text/plain', but expected 'text/xml'.
它给出了错误。“客户端找到的响应内容类型为‘应用程序/八位字节流’,但应为‘文本/xml’。”仅供参考,您是否知道ASMX web服务(您正在使用的)是一种不应用于新开发的旧技术?
[WebMethod()]
public string GetFileByDocumentNumber(string DocumentNumber)
{
string FilePath = GetFile(DocumentNumber);
string FullPath = ConfigurationManager.AppSettings["FilePath"] + FilePath;
return File.ReadAllText(FullPath);
}