WCF 服务发送 .txt 或 .xls,但不发送 .gz(压缩文件)
我有一个 WCF 服务正在运行,它将文件保存到我的服务器。我通过发送 .txt
文件和 .xls
文件对此进行了测试。 .xls
文件的大小为 90kb。
现在我有一个压缩文件 .gz
,大小只有 70KB,但是当尝试完成此操作时,我得到
远程服务器返回意外响应:(400)
错误的请求。
当我尝试搜索此错误请求时,大多数结果都会导致正在发送一个大文件,并且我需要更改配置文件中的设置。
这不是我的情况的原因,因为 .gz
文件比 .xls
文件小!
有谁知道为什么这种类型的文件不能像文本或 Excel 文件一样发送???
编辑:添加代码
这是调用服务的方法
string name = "New Doc";
string file = @"C:/Users/Admin/Desktop/fileZipped.gz";
string ext = file.Split('.')[1];
Stream stream = File.OpenRead(file);
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
stream.Close();
var uploadFileServiceClient = new UploadFileServiceClient();
uploadFileServiceClient.UploadFile(name, bytes, ext);
这是我的服务:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class UploadFileService : IUploadFileService
{
public bool UploadFile(string name, byte[] bytes, string extension)
{
string importDir = ConfigurationSettings.AppSettings["UploadFileDir"];
if (String.IsNullOrEmpty(importDir))
return false;
if (!Directory.Exists(importDir))
return false;
string outputFile = Path.Combine(importDir, Guid.NewGuid() + "." + extension);
string filename = outputFile.Split('\\')[2];
using (StreamWriter sw = new StreamWriter(outputFile))
{
sw.WriteLine(bytes);
sw.Flush();
sw.Close();
}
return true;
}
}
就像我说的,它适用于文本或 Excel 文件。
I have a WCF service up and running which saves a file to my server. I have tested this by sending a .txt
file and a .xls
file. The size of the .xls
file is 90kb.
Now I have a zipped file .gz
that is only 70KB in size but when trying to complete this operation I get
The remote server returned an unexpected response: (400)
Bad Request.
When I have tried to search for this Bad Request most results lead to the fact that a large file is being sent and that I need to change the settings in the config file.
This is not the reason in my case as the .gz
file is smaller than the .xls
file!
Does anyone have any idea why this type of file cannot be sent the same as a text or Excel file???
Edit: Code Added
Here is the method to call the service
string name = "New Doc";
string file = @"C:/Users/Admin/Desktop/fileZipped.gz";
string ext = file.Split('.')[1];
Stream stream = File.OpenRead(file);
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, 0, (int)stream.Length);
stream.Close();
var uploadFileServiceClient = new UploadFileServiceClient();
uploadFileServiceClient.UploadFile(name, bytes, ext);
And here is my service:
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class UploadFileService : IUploadFileService
{
public bool UploadFile(string name, byte[] bytes, string extension)
{
string importDir = ConfigurationSettings.AppSettings["UploadFileDir"];
if (String.IsNullOrEmpty(importDir))
return false;
if (!Directory.Exists(importDir))
return false;
string outputFile = Path.Combine(importDir, Guid.NewGuid() + "." + extension);
string filename = outputFile.Split('\\')[2];
using (StreamWriter sw = new StreamWriter(outputFile))
{
sw.WriteLine(bytes);
sw.Flush();
sw.Close();
}
return true;
}
}
Like I say it works for text or Excel files.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果中间没有“过滤代理”:
我们有类似的东西,解决方案是在客户端+服务器上相应地设置 MIME 类型...
IF there is no "filtering proxy" inbetween:
We had something similar and the solution turned out to setup the MIME type on client+server accordingly...