Windows上的Chrome在下载的文件中添加下划线? [英] Chrome on Windows adding trailing underscores to downloaded files?

查看:428
本文介绍了Windows上的Chrome在下载的文件中添加下划线?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个很奇怪的情况,我很难在现有的Django应用程序中进行跟踪.从APIView继承的其中一个视图在用户进行POST调用时随文件一起返回.端点工作正常,但是当下载的文件到达客户端计算机时,发生了一些奇怪的事情.到浏览器收到文件时,文件扩展名已重命名为结尾的下划线. (因此,假设文件最初是"test.txt",则客户端接收的版本将是"test.txt_").

I've got a rather odd situation happening, that I'm having difficulty tracking down in an existing Django application. One of the views, which inherits from APIView, returns with a file when a user makes a POST call. The endpoint works fine, but there's something odd happening when the downloaded file reaches the client machine. By the time the browser receives the file, the file extension has been renamed with a trailing underscore. (So suppose the file was originally "test.txt", the version that the client receives would be "test.txt_").

据我所知,在APIView中返回响应对象之前,content-type和content-disposition标头看起来是正确的.例如:

As near as I can figure, just before the response object is returned in the APIView, the content-type and content-disposition headers look correct. E.g.:

Content-Type: application/octet-stream
Content-Disposition: attachment;filename="test.txt"

在Chrome下载中显示该文件时,该文件名为"test.txt_"-带有下划线.我已经在Firefox中尝试过相同的操作,并且似乎可以正确下载.不幸的是,告诉大多数用户切换浏览器是行不通的.

That same file, when it shows up in Chrome downloads, is named "test.txt_" - with the trailing underscore. I've tried the same thing out in Firefox, and it seems to download correctly. Unfortunately, telling the majority of our users to switch browsers isn't going to fly.

我尝试过:

  • 强制使用不同的内容类型(例如:代替"application/octet-stream",而尝试"application/text",只是为了看看会发生什么).这没有效果.
  • 格式化内容的方式略有不同(例如:分号和文件名之间的空格).这也没有效果.
  • 删除了content-disposition标头中文件名周围的双引号.没有效果.
  • 在Rest Framework本身中放置断点,但是Visual Studio Code似乎并未触发这些断点. (我不太熟悉通过Visual Studio Code进行调试,所以这可能是我的错.)
  • 剔除任何自定义中间件,因此剩下的唯一中间件如下:
corsheaders.middleware.CorsMiddleware
django.contrib.sessions.middleware.SessionMiddleware
django.middleware.locale.LocaleMiddleware
django.middleware.common.CommonMiddleware
django.middleware.csrf.CsrfViewMiddleware
django.contrib.auth.middleware.AuthenticationMiddleware
django.contrib.messages.middleware.MessageMiddleware

到目前为止,其他人遇到的任何类似问题似乎都略有不同(即:Internet Explorer删除了扩展名中的句点,并用下划线代替了它.)

So far, any similar issues that other people have experienced seem to be slightly different (i.e.: Internet Explorer removing the period in the extension and replacing it with an underscore).

有人猜测这里可能会发生什么?我有点难过.

Any guesses on what might be happening here? I'm a bit stumped.

推荐答案

我们的环境中有一个文档存储系统,其中包含具有DocumentName和ContentType属性的文档.在某些情况下,内容类型会返回,并在字符串的末尾附加空格,例如"pdf".

Our environment has a document storage system that contains documents with the attributes DocumentName and ContentType. In some cases, the content type would return with spaces appended to the end of the string like "pdf ".

在Internet Explorer中,输出将截断字符串的末尾,而Chrome会将多余的空格转换为下划线,并为我提供以下文件名:"file.pdf ________________"

In Internet Explorer the output would truncate the end of the string while Chrome would convert the extra spaces to underscores giving me this filename: "file.pdf______________"

要解决这个问题,我只是截断了字符串.

To resolve I simply truncate the string.

public string getFileName(string docName, string contentType) {
    string fileName = docName + "." + contentType.Trim();

    return fileName;
}

这篇关于Windows上的Chrome在下载的文件中添加下划线?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆