1

I want to use ng-file-upload to upload files to hard drive in the server

  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));
                                        });
                                    });
                                }

when I use this code I get this error

angular.js:10765 XMLHttpRequest cannot load file:///C:/Javalogs. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https, chrome-extension-resource.

that what I did but it didn't work

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);
                                    }
                                }) 

the picture is generated but I can't view it

Belal Othman
  • 231
  • 3
  • 12

1 Answers1

0

I send them via a base64 url to the backend. It depends on which data you need but it should roughly look like the following code.

 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 is a service of ng-file-upload, which loads the file to the browser as a base64 url. I hope I could help you.

Thomas Zoé
  • 423
  • 3
  • 16