javascript - 将文件内容存储在表中(html table tr td) jquery

标签 javascript php jquery ajax blob

我想通过将每个文件内容上传/添加到表中来在数据库中插入多个文件,然后将所有文件添加到表中后,我添加表的值,包括我传递给ajax的表单数据中的文件内容。我试过了

var file = $('#file')[0].files[0];
//var file = document.getElementById('file').files[0];
var name = file.name;
var size = file.size;
var type = file.type;
var blob = new Blob([file], {
    type: "application/octet-binary"
});
var filecontent = blob;

Here i used the value of filecontent to be passed to ajax but in php the condition if(!empty($_FILES) remaind false)

在console.log文件内容看起来像[object Blob]我想做的是实现我的主要目标,即存储到数据库,我已经问过一个关于它的问题。可以找到我here这就是我问第一个问题的原因。

我的问题是如何将文件内容存储到变量中,在本例中存储 $('#file')[0].files[0] 的值至file .

UPDATE

当我使用 var file = $('#file')[0].files[0]; 的值时我仍然遇到同样的错误

UPDATE

是否可以存储 input:file进入table tdinputany other element并在需要时重用该文件

最佳答案

要将文件内容存储到变量中,您首先需要读取它们,因此您必须使用 FileReader

例如readAsText()方法语法:

instanceOfFileReader.readAsText(blob[, encoding]);

The readAsText method is used to read the contents of the specified Blob or File. When the read operation is complete, the readyState is changed to DONE, the loadend is triggered, and the result attribute contains the contents of the file as a text string.

因此您可以将结果存储到变量中。

关于javascript - 将文件内容存储在表中(html table tr td) jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29179236/

相关文章:

php - SQL - 如何获取创建的同一日期的最新数据

php - 如何构造 HTML 表单元素以便将它们轻松转换为 PHP 对象?

javascript - 按状态获取订单 Firebase

javascript找到深度嵌套的对象

javascript - Word 联机加载项 : Using objects across multiple contexts

php - 使用 PHP 生成的 Web 表单和 mySQL 更新表中的多行

javascript - 添加到 url 的 Python key

javascript - 如何构造jquery ajax以灵活输入和输出?

jquery - 动画眼睛 - 为什么滚动时它们会跟随?

javascript - 如何使用javascript在后台打开弹出窗口?