我目前正在研究一个显示由C++服务器发送的图像的WebSocket应用程序.我在那里看到了几个主题,但我似乎无法在Firefox中摆脱这个错误:
图像损坏或截断:data:image/png; base64,[some data]
这是我用来显示我的blob的Javascript代码:
socket.onmessage = function(msg) {
var blob = msg.data;
var reader = new FileReader();
reader.onloadend = function() {
var string = reader.result;
var buffer = Base64.encode(string);
var data = "data:image/png;base64,"+buffer;
var image = document.getElementById('image');
image.src = data;
};
reader.readAsBinaryString(blob);
}
Run Code Online (Sandbox Code Playgroud)
我正在使用我在这个主题上找到的红点图片:https://stackoverflow.com/a/4478878/1464608 而Base64类来自这里:https://stackoverflow.com/a/246813/ 1464608
但我获得的base64结果不匹配,Firefox检索到图像被损坏的错误.
我知道这不是很多信息,但我不知道在哪里看:/任何帮助都非常受欢迎!
在我们的应用程序中,我们为用户提供了将文档上传到windows azure blob存储帐户的能力.上传文档或图像后,它会被分配一些网址(https://name.blob.core.windows.net/container/file-name.jpg).如果文档是图像或pdf或某些可由浏览器呈现的文件,我们试图在浏览器中显示它而无需用户下载文件.如果我们只是打开一个新窗口或选项卡并将用户引导到IE中的blob uri,则图像或pdf会在浏览器中正确呈现.但是,如果我们尝试在Chrome,FireFox或Safari中打开一个指向uri的新窗口,它只会下载文件而不是在浏览器中显示它.
有没有办法强制后三个浏览器只显示文件而不是下载它?
我编写了使用Angular $ http下载文件的代码.URL中未指定文件的名称.URL包含文件的唯一标识符,该标识符从应用程序外部获取.
什么时候$http.get(myUrl)叫,一切正常; 检索文件,我可以在我的回调处理程序中访问它,但我看不到如何获取文件的名称.用Fiddler捕获原始响应,我看到了这个:
HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 54
Content-Type: application/octet-stream
Server: Microsoft-IIS/8.5
Access-Control-Allow-Origin: http://www.example.com/getFile/12345
Access-Control-Allow-Credentials: true
Content-Disposition: attachment; filename=testfile.txt
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Fri, 09 Oct 2015 20:25:49 GMT
Lorem ipsum dolar sit amet! The contents of my file!
Run Code Online (Sandbox Code Playgroud)
从上面可以看出,服务器显然是在"Content-Disposition"中发回文件的名称,但我还没有找到在Angular回调中访问它的方法.如何从标题中获取文件的名称?
编辑以回答以下答案:
我之前应该提到我已经尝试过response.headers().它返回Object {content-type: "application/octet-stream", cache-control: "private"},所以我仍然没有得到Content-Disposition因为某些原因. response.headers('Content-Disposition')回报null.
我需要帮助在Hibernate中加载延迟blob.我在我的Web应用程序中有这些服务器和框架:MySQL,Tomcat,Spring和Hibernate.
数据库配置的一部分.
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
<property name="user" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<property name="driverClass" value="${jdbc.driverClassName}"/>
<property name="jdbcUrl" value="${jdbc.url}"/>
<property name="initialPoolSize">
<value>${jdbc.initialPoolSize}</value>
</property>
<property name="minPoolSize">
<value>${jdbc.minPoolSize}</value>
</property>
<property name="maxPoolSize">
<value>${jdbc.maxPoolSize}</value>
</property>
<property name="acquireRetryAttempts">
<value>${jdbc.acquireRetryAttempts}</value>
</property>
<property name="acquireIncrement">
<value>${jdbc.acquireIncrement}</value>
</property>
<property name="idleConnectionTestPeriod">
<value>${jdbc.idleConnectionTestPeriod}</value>
</property>
<property name="maxIdleTime">
<value>${jdbc.maxIdleTime}</value>
</property>
<property name="maxConnectionAge">
<value>${jdbc.maxConnectionAge}</value>
</property>
<property name="preferredTestQuery">
<value>${jdbc.preferredTestQuery}</value>
</property>
<property name="testConnectionOnCheckin">
<value>${jdbc.testConnectionOnCheckin}</value>
</property>
</bean>
<bean id="lobHandler" class="org.springframework.jdbc.support.lob.DefaultLobHandler" />
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="/WEB-INF/hibernate.cfg.xml" />
<property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
<property name="hibernateProperties"> …Run Code Online (Sandbox Code Playgroud) 我有一个表,图像数据存储在MySQL数据库的blob字段中.有没有办法只使用SQL将这些图像导出到文件系统上的文件?图像应命名为{imageId} .jpg
我知道用Java或其他方法很容易做到这一点,但只用SQL脚本就可以了吗?
我正在尝试在blob列中编写和更新pdf文档,但我只能更新blob,只写入比以前存储的数据更多的数据.如果我尝试用较小的文档数据更新blob列,我只会得到一个损坏的pdf.
首先使用empty_blob()函数初始化blob列.我在下面编写了示例Java类来测试此行为.我第一次以'true'作为main方法的第一个参数运行它,所以在第一行中存储了大约31kB的文档,在第二行中存储了一个278kB的文档.然后我用'false'作为参数运行它,这样两行应该更新交换文件.结果是,只有当我写入比现有数据更多的数据时才能得到正确的结果.
如何编写一种以可靠的方式编写和更新blob的方法而不必担心二进制数据的大小?
import static org.apache.commons.io.IOUtils.copy;
import java.io.FileInputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import oracle.jdbc.OracleDriver;
import oracle.jdbc.OracleResultSet;
import oracle.sql.BLOB;
import org.apache.commons.lang.ArrayUtils;
/**
* Prerequisites:
* 1) a table named 'x' must exists [create table x (i number, j blob);]
* 2) that table should have two columns [insert into x (i, j) values (1, empty_blob()); insert into x (i, j) values (2, empty_blob()); commit;]
* 3) download lsp.pdf from http://www.objectmentor.com/resources/articles/lsp.pdf
* …Run Code Online (Sandbox Code Playgroud) 我有一个大约120k行的表,其中包含一个带有BLOB的字段(每个条目的大小不超过1MB,通常要少得多).我的问题是每当我运行查询询问此表上的任何列(不包括BLOB)时,如果文件系统缓存为空,则需要大约40'才能完成.同一个表上的所有后续查询都需要少于1''(从命令行客户端,在服务器本身上进行测试).查询中返回的行数从空集到60k +不等
我已经删除了查询缓存,因此它与它无关.该表是myisam,但我也尝试将其更改为innodb(并设置ROW_FORMAT = COMPACT),但没有任何运气.
如果我删除BLOB列,查询总是很快.
所以我假设服务器从磁盘(或其中的一部分)读取blob,文件系统缓存它们.问题是在流量高且内存有限的服务器上,文件系统缓存每隔一段时间刷新一次,因此这个特定的查询一直给我带来麻烦.
所以我的问题是,有没有办法大幅加快速度,而无需从表中删除blob列?
这里有两个示例查询,一个接一个地运行,以及解释,索引和表定义:
mysql> SELECT ct.score FROM completed_tests ct where ct.status != 'deleted' and ct.status != 'failed' and score < 100;
Empty set (48.21 sec)
mysql> SELECT ct.score FROM completed_tests ct where ct.status != 'deleted' and ct.status != 'failed' and score < 99;
Empty set (1.16 sec)
mysql> explain SELECT ct.score FROM completed_tests ct where ct.status != 'deleted' and ct.status != 'failed' and score < 99;
+----+-------------+-------+-------+---------------+--------+---------+------+-------+-------------+
| id | select_type | …Run Code Online (Sandbox Code Playgroud) 我正在尝试实现一个粘贴处理程序来从用户的剪贴板中获取图像.我希望这只能在Google Chrome上运行,我并不担心其他浏览器.
这是我在互联网上找到的一种方法的一部分,我正在努力调整它.
// Get the items from the clipboard
var items = e.clipboardData.items;
if (items) {
// Loop through all items, looking for any kind of image
for (var i = 0; i < items.length; i++) {
if (items[i].type.indexOf("image") !== -1) {
// We need to represent the image as a file,
var blob = items[i].getAsFile();
// and use a URL or webkitURL (whichever is available to the browser)
// to create a temporary URL to the object
var …Run Code Online (Sandbox Code Playgroud) 我正在下载带有axios的 zip 文件。为了进一步处理,我需要获取已下载的“原始”数据。据我所知,在 Javascript 中有两种类型:Blob 和 Arraybuffers。两者都可以responseType在请求选项中指定。
在下一步中,需要解压缩 zip 文件。我为此尝试了两个库:js-zip 和 adm-zip。两者都希望数据是一个 ArrayBuffer。到目前为止一切顺利,我可以将 blob 转换为缓冲区。在此转换之后,adm-zip 总是很高兴地提取 zip 文件。但是,js-zip 会抱怨文件损坏,除非 zip 已'arraybuffer'作为 axios下载responseType。js-zip 不适buffer用于从blob.
这让我很困惑。我想这两个ArrayBuffer和Blob本质上的底层内存只是意见。将某些内容下载为 blob 与缓冲区之间可能存在性能差异。但是结果数据应该是一样的吧?
好吧,我决定进行实验并发现:
如果指定responseType: 'blob',axios 会将 转换response.data为字符串。假设您对该字符串进行哈希处理并获得哈希码 A。然后将其转换为缓冲区。对于这种转换,您需要指定一种编码。根据编码的不同,您将获得各种新的哈希值,我们称它们为 B1、B2、B3,...当指定 'utf8' 作为编码时,我将返回原始哈希值 A。
所以我猜当下载数据为 a 时'blob',axios 隐式地将其转换为用 utf8 编码的字符串。这似乎非常合理。
现在您指定responseType: 'arraybuffer'. Axios 为您提供了一个缓冲区作为response.data. 对缓冲区进行哈希处理,您会得到一个哈希码 C。此代码与 A、B1、B2 中的任何代码都不对应,...
那么当下载数据为 时'arraybuffer',你得到完全不同的数据? …
我想复制一个REST响应成团块,但我不能做一些,因为blob()和arrayBuffer()尚未响应对象已经实施.Response Body是一个私有变量.
...
return this.http.get(url, {params: params, headers: headers})
.map(res => {
// can't access _body because it is private
// no method appears to exist to get to the _body without modification
new Blob([res._body], {type: res.headers.get('Content-Type')});
})
.catch(this.log);
...
Run Code Online (Sandbox Code Playgroud)
在实施这些方法之前,我是否可以使用解决方案?
blob ×10
javascript ×3
mysql ×3
image ×2
java ×2
angular ×1
angularjs ×1
arraybuffer ×1
axios ×1
azure ×1
base64 ×1
caching ×1
export ×1
filereader ×1
firefox ×1
hibernate ×1
html5 ×1
http-headers ×1
httpresponse ×1
jdbc ×1
lazy-loading ×1
node.js ×1
oracle ×1
performance ×1
rest ×1
sql ×1
websocket ×1