我正在尝试显示 favicon.ico在网页上不是作为快捷方式图标,而是作为页面正文中的图像。在我们的 IE 测试服务器上,图像无法显示,我们发现这是因为服务器上为 .ico 配置的 MIME 类型。文件类型为 image/vnd.microsoft.icon而不是 image/x-icon .
现在,我们能够重新配置服务器并解决问题,但我想知道是否可以在 <img> 中指定要使用的 MIME 类型标记并覆盖特定文件的服务器范围设置?
最佳答案
好消息是,如果您不能依赖服务器提供的类型,则可以强制覆盖图像的 MIME 类型。坏消息是它依赖于 Javascript,并且有点 hacky。
我想使用存储在我的 Gitorious 中的文件HTML 图像标签中的存储库。但是,“原始”文件数据被标记为 text/plain由服务器阻止 Internet Explorer 显示它们。 Firefox 和 Chrome 运行良好,因此我假设它们必须忽略提供的 MIME 类型并根据图像数据找出实际格式。
您不能为 <img> 显式指定 MIME 类型标签。一方面,<img>标签没有 type属性(不同于 <object> 和 <embed> ,或 <source> 和 <audio> 元素使用的 <video> 标签)。即使他们这样做了,there's no guarantee that it would make any difference :
This specification does not currently say whether or how to check the MIME types of the media resources, or whether or how to perform file type sniffing using the actual file data. Implementors differ in their intentions on this matter and it is therefore unclear what the right solution is. In the absence of any requirement here, the HTTP specification's strict requirement to follow the Content-Type header prevails ("Content-Type specifies the media type of the underlying data." ... "If and only if the media type is not given by a Content-Type field, the recipient MAY attempt to guess the media type via inspection of its content and/or the name extension(s) of the URI used to identify the resource.").
图像数据可以通过XMLHttpRequest“手动”下载。 .一旦实际数据可用于 Javascript,就可以通过 DOM 操作将其插入页面。这是一个示例 HTML 文件:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Hello, World!</title>
<script src="ieimgfix.js"></script>
</head>
<body>
<img alt="cat" src="https://gitorious.org/vector/vector/raw/0797c6f8faad3426d33d3748b07abd8c77d475a7:bin/media/Floyd-Steinberg_algorithm-original.jpg">
<img alt="apple" src="https://gitorious.org/nanijsore/nanijsore/raw/34b9aae73b5623b9971c8d98878fdbb2a0264476:image/apple.png">
</body>
</html>
...这是 ieimgfix.js 的内容文件:
"use strict";
// This function is called when any image tag fails to load.
function fixMIME()
{
var img = this;
// First of all, try to guess the MIME type based on the file extension.
var mime;
switch (img.src.toLowerCase().slice(-4))
{
case ".bmp": mime = "bmp"; break;
case ".gif": mime = "gif"; break;
case ".jpg": case "jpeg": mime = "jpeg"; break;
case ".png": case "apng": mime = "png"; break;
case ".svg": case "svgz": mime = "svg+xml"; break;
case ".tif": case "tiff": mime = "tiff"; break;
default: console.log("Unknown file extension: " + img.src); return;
}
console.log("Couldn't load " + img.src + "; retrying as image/" + mime);
// Attempt to download the image data via an XMLHttpRequest.
var xhr = new XMLHttpRequest();
xhr.onload = function()
{
if (this.status != 200) { return console.log("FAILED: " + img.src); }
// Blob > ArrayBuffer: http://stackoverflow.com/a/15981017/4200092
var reader = new FileReader();
reader.onload = function()
{
// TypedArray > Base64 text: http://stackoverflow.com/a/12713326/4200092
var data = String.fromCharCode.apply(null, new Uint8Array(this.result));
img.src = "data:image/" + mime + ";base64," + btoa(data);
};
reader.readAsArrayBuffer(this.response);
};
xhr.open("get", this.src, true);
xhr.responseType = "blob";
xhr.send();
}
// This callback happens after the DOCUMENT is loaded but before IMAGES are.
document.addEventListener("readystatechange", function() {
if (document.readyState != "interactive") { return; }
// Add an error handler callback to all image tags in the document.
var t = document.getElementsByTagName("img");
for (var i = 0; i < t.length; ++i) { t[i].addEventListener("error", fixMIME, false); }
}, false);
请记住,任何新 <img>不包括通过 DOM 操作添加到页面的标记,因此您需要自己将监听器附加到这些标记。
有趣的是,上面的代码导致 Firefox 和 Chrome 都提示 CORS处理无效图片 URL 时:
XMLHttpRequest cannot load http://www.google.com/notarealfile.png. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'null' is therefore not allowed access. The response had HTTP status code 404.
Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://www.google.com/notarealfile.png. This can be fixed by moving the resource to the same domain or enabling CORS.
然而,Internet Explorer 11 似乎并不关心。这很有效:
XMLHttpRequest需要制作。XMLHttpRequest将在没有 CORS 相关问题的情况下运行。免责声明:正确的做法是让服务器发送正确的 MIME 类型。这是一个相当棘手的解决方法,除非您真的别无选择,否则我不会推荐它。
关于html - 我可以在 img 标签中明确指定 MIME 类型吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23115581/
类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc
我想将html转换为纯文本。不过,我不想只删除标签,我想智能地保留尽可能多的格式。为插入换行符标签,检测段落并格式化它们等。输入非常简单,通常是格式良好的html(不是整个文档,只是一堆内容,通常没有anchor或图像)。我可以将几个正则表达式放在一起,让我达到80%,但我认为可能有一些现有的解决方案更智能。 最佳答案 首先,不要尝试为此使用正则表达式。很有可能你会想出一个脆弱/脆弱的解决方案,它会随着HTML的变化而崩溃,或者很难管理和维护。您可以使用Nokogiri快速解析HTML并提取文本:require'nokogiri'h
Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack
使用带有Rails插件的vim,您可以创建一个迁移文件,然后一次性打开该文件吗?textmate也可以这样吗? 最佳答案 你可以使用rails.vim然后做类似的事情::Rgeneratemigratonadd_foo_to_bar插件将打开迁移生成的文件,这正是您想要的。我不能代表textmate。 关于ruby-使用VimRails,您可以创建一个新的迁移文件并一次性打开它吗?,我们在StackOverflow上找到一个类似的问题: https://sta
查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html
我试图使用yard记录一些Ruby代码,尽管我所做的正是所描述的here或here#@param[Integer]thenumberoftrials(>=0)#@param[Float]successprobabilityineachtrialdefinitialize(n,p)#initialize...end虽然我仍然得到这个奇怪的错误@paramtaghasunknownparametername:the@paramtaghasunknownparametername:success然后生成的html看起来很奇怪。我称yard为:$yarddoc-mmarkdown我做错了什么?
在我的Controller中,我通过以下方式在我的index方法中支持HTML和JSON:respond_todo|format|format.htmlformat.json{renderjson:@user}end在浏览器中拉起它时,它会自然地以HTML呈现。但是,当我对/user资源进行内容类型为application/json的curl调用时(因为它是索引方法),我仍然将HTML作为响应。如何获取JSON作为响应?我还需要说明什么? 最佳答案 您应该将.json附加到请求的url,提供的格式在routes.rb的路径中定义。这
我发现ActiveRecord::Base.transaction在复杂方法中非常有效。我想知道是否可以在如下事务中从AWSS3上传/删除文件:S3Object.transactiondo#writeintofiles#raiseanexceptionend引发异常后,每个操作都应在S3上回滚。S3Object这可能吗?? 最佳答案 虽然S3API具有批量删除功能,但它不支持事务,因为每个删除操作都可以独立于其他操作成功/失败。该API不提供任何批量上传功能(通过PUT或POST),因此每个上传操作都是通过一个独立的API调用完成的
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)