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

using ng-file-upload to upload file to hard drive

我想使用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);
                                    }
                                }) 

图片已生成,但我无法查看

我通过 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 
  }
}

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