在javascript中读取文件的Blob并插入数据库

时间:2017-07-10 04:56:52

标签: javascript mysql database string blob

我想从我的js代码中读取任何类型的文件,并将其blob插入到长blob类型列中的数据库中。 js中的变量是将blob读取为字符串,而不是blob。

所以如果有单/双引号等特殊字符,我将面临问题。

阅读代码

function onChooseFile(event, onLoadFileHandler) {
if (typeof window.FileReader !== 'function')
    throw ("The file API isn't supported on this browser.");
let input = event.target;
if (!input)
    throw ("The browser does not properly implement the event object");
if (!input.files)
    throw ("This browser does not support the `files` property of the file input.");
if (!input.files[0])
    return undefined;
let file = input.files[0];
let fr = new FileReader();
fr.onload = onLoadFileHandler;
fr.onloadend = function(event) {
  if (event.target.readyState == FileReader.DONE) { // DONE == 2
    //blobData = event.target.result;
    blobData = new Blob([event.target.result], { type: fileType });

    console.log(blobData);
    console.log(typeof(blobData));
    console.log(blobData instanceof Blob) 
    console.log("-------------------------------------------------");

    blobString = ab2str(event.target.result);
    console.log(blobData);
    alert("file read complete "+blobData.length);
  }
};
document.getElementById('inputHeader').value = file.name;
  fileName = file.name;
  fileType = file.type;
  fileExtension = fileName.split(".").pop();
  fr.readAsBinaryString(file);
}

编写代码

dataservice.openDocument(document_version_id)
            .done(function (reply) {
                fileExtension = fileName.split(".").pop();
                switch(fileExtension.toLowerCase()){
                    case "doc":
                    case "docx":
                    fileType = "application/msword";
                    break;
                    case "pdf":
                    fileType = "application/pdf";
                    break;
                    case "xls":
                    case "xlsx" :
                    fileType = "application/vnd.ms-excel";
                    break;
                }

                var blob = new Blob([reply.RECORD_DATA.LONG_BLOB], { type: fileType });
                saveAs(blob, 'C:/OutputFile/hello.'+fileExtension);
                deferred.resolve();
            }).fail(function (error) {
                alert("failure in getting document");
                deferred.reject();
            });
        });

请帮助,如何实现这一点。

由于

0 个答案:

没有答案