Working with the file: // and Angular protocol

2

I wanted to know if it is possible to work with a Http: // request locally in my application and not on a local server, if at all possible how do I do this? (does not necessarily have to be with the file: // protocol).

//Cleanup the popover when we're done with it!
  $scope.$on('$destroy', function() {
    $scope.musicActionspopover.remove();
  });

  $scope.openMusicActions = function($event, music) {
    $scope.audioSelected = music;
    $scope.musicActionspopover.show($event);
  };

  $scope.closeMusicActionsPopover = function() {
    $scope.musicActionspopover.hide();
  };

  $scope.playAudio = function($event, audioSelected) {
    // Prevents start the song when it is clicked on the menu
    if ($event.srcElement.tagName == 'I') {
      $event.preventDefault();
      return;
    }

    var audioSelectedIndex = 0;
    var queueMusics = [];

    angular.forEach($scope.playlist.audios, function (music, index) {
      if (audioSelected.nid == music.nid) {
        audioSelectedIndex = index;
      };

        queueMusics.push({
        id: music.nid, 
        name: music.title, 
        artist: music.interpeters[0], 
        audioUrl: "file://audio/0379442681742547b1cc4baacab409c3/320/ts_.m3u8",//music.audio_url, 
        albumId: music.album.nid, 
        albumCover: music.album.cover, 
        albumBlurCover: music.album.blur_cover, 
        audioType: music.audioType,
        collectionSourceID: $scope.playlist.nid,
        collectionSourceTitle: $scope.playlist.title,
        collectionSourceCategory: "System Playlist"
      });
    });

    $rootScope.$emit("playAudio", {index: audioSelectedIndex, queueName: $scope.playlist.title, audios: queueMusics});
    $scope.closeMusicActionsPopover();
  };
  $scope.playSystemPlaylist = function ($event) {

    playlist = $scope.playlist;
    var audioSelectedIndex = 0;
    var queueMusics = [];

    angular.forEach(playlist.audios, function (music, index) {
      queueMusics.push({
        id: music.nid, 
        name: music.title, 
        artist: music.interpeters[0], 
        audioUrl: music.audio_url, 
        albumId: music.album.nid, 
        albumCover: music.album.cover, 
        albumBlurCover: music.album.blur_cover,
        audioType: music.audioType,
        collectionSourceID: $scope.playlist.nid,
        collectionSourceTitle: $scope.playlist.title,
        collectionSourceCategory: "System Playlist"
      });
    });

    $rootScope.$emit("playAudio", {index: audioSelectedIndex, queueName: playlist.title, audios: queueMusics});
  };

}]);

I'm currently experiencing this error:

  

clappr.js: 30025 XMLHttpRequest can not load file: //audio/0379442681742547b1cc4baacab409c3/320/ts_.m3u8. Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.   loadInternal @ clappr.js: 30025   d @ raven.js: 1279

    
asked by anonymous 12.04.2017 / 15:04

2 answers

1

The problem is security.

  

clappr.js: 30025 XMLHttpRequest can not load file: //audio/0379442681742547b1cc4baacab409c3/320/ts_.m3u8. Cross source requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https. loadInternal @ clappr.js: 30025 d @ raven.js: 1279

What happened here is that a page tried to access something outside its domain, about a protocol in which this is not possible (the supported protocols are listed in the error message).

You must use one of the specified protocols. In addition, you'll probably have to configure the application or the server to allow unsecured requests. And it will have a large margin for application security flaws.

Perhaps this is the case for rethinking the application architecture and how its content will be served. I suggest opening a specific question about avoiding Cross origin requests in your application, when you hold both the requesting page and the content to be served on the same machine. So you can do what you have to do with less code, less settings, and your application will be more secure.

    
12.04.2017 / 16:09
1

Simple answer: You can not .

The file:// protocol has several restrictions imposed by the browser.

However, nothing prevents you from creating a web service on your machine, and offers a local site accessible by a URL similar to http://localhost .

Some examples of simple http server implementation below:

  • Python - python -m SimpleHTTPServer 8080
  • PHP - php -S localhost:8080
  • NodeJS - npm install -g http-server , followed by http-server
13.04.2017 / 16:18