使用ng-file-upload将文件上传到硬盘

问题描述 投票:1回答:1

我想使用ng-file-upload将文件上传到服务器中的硬盘

  this.uploadFiles = function(files, errFiles) {
                                  this.files = files;
                                  this.errFiles = errFiles;
                                    angular.forEach(files, function(file) {
                                        file.upload = Upload.upload({
                                            url: 'C:\Java\logs',
                                            data: {file: file}
                                        });

                                        file.upload.then(function (response) {
                                            $timeout(function () {
                                                file.result = response.data;
                                            });
                                        }, function (response) {
                                            if (response.status > 0)
                                                this.errorMsg = response.status + ': ' + response.data;
                                        }, function (evt) {
                                            file.progress = Math.min(100, parseInt(100.0 * 
                                                                     evt.loaded / evt.total));
                                        });
                                    });
                                }

当我使用这段代码时,我得到了这个错误

angular.js:10765 XMLHttpRequest无法加载file:/// C:/ Javalogs。 交叉源请求仅支持协议方案:http,data,chrome,chrome-extension,https,chrome-extension-resource。

我做了什么,但它没有奏效

this.uploadFiles = function(file, errFiles) {
                                Upload.dataUrl(file[0], true).then(function (base64Url) {
                                    var filename = 'test.jpg';
                                     var a = document.createElement("a");
                                    var myDocument = getFileContainer(base64Url);
                                    var blob = new Blob([myDocument.data], {type: 'image/jpg'});
                                    var file = new File([blob], 'imageFileName.jpg');
                                    if(window.navigator.msSaveOrOpenBlob) {
                                        window.navigator.msSaveBlob(blob, filename);
                                    }
                                    else{
                                        var elem = window.document.createElement('a');
                                        elem.href = window.URL.createObjectURL(file);
                                        elem.download = filename;        
                                        document.body.appendChild(elem);
                                        elem.click();        
                                        document.body.removeChild(elem);
                                    }
                                }) 

图片生成但我无法查看

angularjs ng-file-upload
1个回答
0
投票

我通过base64 url​​将它们发送到后端。 这取决于您需要哪些数据,但它应该大致如下所示。

 function getMimeTypeFromUrl(imageUrl) {
  return imageUrl.substring(imageUrl.indexOf(':') + 1, imageUrl.indexOf(';'));
}

function getDataFromUrl(imageUrl){
  return imageUrl.substring(imageUrl.indexOf(',') + 1);
}

function getFileContainer(imageUrl, file){
  return {
    data:getDataFromUrl(imageUrl),
    mimeType: getMimeTypeFromUrl(imageUrl),
    filename: file.name,
  }
}
this.uploadFiles = function(files, errFiles) {
Upload.dataUrl(file, true).then(function (base64Url) {
  myDocument = getFileContainer(base64Url, file);
   //here you have to post myDocument 
  }
}

Upload是ng-file-upload的服务,它将文件作为base64 url​​加载到浏览器。 我希望我能帮助你。

© www.soinside.com 2019 - 2024. All rights reserved.