WCF 服务发送 .txt 或 .xls,但不发送 .gz(压缩文件)

发布于 2024-11-30 02:06:57 字数 1798 浏览 0 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

后来的我们 2024-12-07 02:06:57

如果中间没有“过滤代理”:

我们有类似的东西,解决方案是在客户端+服务器上相应地设置 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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文