bitfront-library
Version:
Angular CLI project with components and classes used by other Angular projects of the BIT foundation.
62 lines • 2.93 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.UploadService = void 0;
var core_1 = require("@angular/core");
var http_utils_service_1 = require("./http-utils.service");
var i0 = require("@angular/core");
var UploadService = /** @class */ (function () {
function UploadService(apiUrl) {
this.apiUrl = apiUrl;
this.UPLOAD_URL = this.apiUrl + "ficheros/upload";
this.DOWNLOAD_URL = this.apiUrl + "ficheros/download/";
console.log("UploadService creado con API_URL:", this.apiUrl);
}
/**
* servicio para hacer upload de ficheros al servidor
* @param url indica la URL a la que queremos subir el fichero. Si es null se utilizará el endpoint por defecto definido en UPLOAD_URL
* @param files fichero(s) a subir al servidor
* @param params parámetros adicionales en forma de Map para hacer la subida
*/
UploadService.prototype.upload = function (url, files, params) {
var _this = this;
return new Promise(function (resolve, reject) {
var formData = new FormData();
for (var key in params) {
formData.append(key, params[key]);
}
var xhr = new XMLHttpRequest();
for (var i = 0; i < files.length; i++) {
formData.append("file", files[i], files[i].name);
}
xhr.onreadystatechange = function () {
if (xhr.readyState === 4) {
if (xhr.status === 200) {
resolve(xhr.response);
}
else {
reject(xhr.response); //TODO: No llega a los componentes
}
}
};
xhr.open("POST", url == null ? _this.UPLOAD_URL : url, true);
if (http_utils_service_1.HttpUtilsService.LOGIN_AUTHENTICATION && http_utils_service_1.HttpUtilsService.LOGIN_AUTHENTICATION != "") {
xhr.setRequestHeader("Authorization", "Basic " + http_utils_service_1.HttpUtilsService.LOGIN_AUTHENTICATION);
}
xhr.send(formData);
});
};
UploadService.prototype.getUrlDownloadFile = function (id) {
return "" + this.DOWNLOAD_URL + id;
};
UploadService.ɵfac = function UploadService_Factory(t) { return new (t || UploadService)(i0.ɵɵinject("API_URL")); };
UploadService.ɵprov = i0.ɵɵdefineInjectable({ token: UploadService, factory: UploadService.ɵfac });
return UploadService;
}());
exports.UploadService = UploadService;
(function () { (typeof ngDevMode === "undefined" || ngDevMode) && i0.ɵsetClassMetadata(UploadService, [{
type: core_1.Injectable
}], function () { return [{ type: undefined, decorators: [{
type: core_1.Inject,
args: ["API_URL"]
}] }]; }, null); })();
//# sourceMappingURL=upload.service.js.map