Este codigo funciona bien para cargar un archivo pero me da problemas para cargarlo directamente desde sp ¿Es cuestion de permisos?
function createFileInput(callback) {
const file = document.createElement('input');file.setAttribute("type", "file");file.setAttribute("class", "oculto");file.setAttribute("id", "fileInput");
document.body.appendChild(file);
const handleFileChange = async (event) => {try {if (event.target.files.length > 0) {callback();}} catch (error) {console.error('Error reading file:', error);} finally {document.body.removeChild(file);}};
const handleFileCancel = () => {document.body.removeChild(file);};
file.addEventListener('change', handleFileChange);file.addEventListener('cancel', handleFileCancel);file.click();};function encryptFile(clave) {const fileInput = document.getElementById('fileInput');const file = fileInput.files[0];if (file) {const reader = new FileReader();reader.onload = function (event) {const data = event.target.result;const secretKey = clave || 'tu_clave_secretaa';const encryptedData = xorEncrypt(data, secretKey);descargarArchivo(encryptedData)};reader.readAsText(file);} else {console.error('Por favor, selecciona un archivo.');}};function xorEncrypt(data, key) {let result = '';for (let i = 0; i < data.length; i++) {result += String.fromCharCode(data.charCodeAt(i) ^ key.charCodeAt(i % key.length));}return result;};function descargarArchivo(contenido) {// Crea un objeto Blob con el contenido en formato de textovar blob = new Blob([contenido], { type: 'text/plain' });
// Crea un enlace para descargar el archivovar enlaceDescarga = document.createElement('a');enlaceDescarga.href = window.URL.createObjectURL(blob);enlaceDescarga.download = 'miArchivo.txt';
// Añade el enlace al documento y simula el clic para iniciar la descargadocument.body.appendChild(enlaceDescarga);enlaceDescarga.click();
// Limpia el enlace después de la descargadocument.body.removeChild(enlaceDescarga);};function startEncrypt() {createFileInput(encryptFile);};async function obtenerArchivoDesdeSharePoint() {
var archivoURL = "https://sitio/Documentos%20compartidos/miArchivo.txt";try {
var respuesta = await fetch(archivoURL, {method: "GET",headers: {"Accept": "application/json;odata=verbose",}});if (respuesta.ok) {var contenidoArchivo = await respuesta.text();var blob = new Blob([contenidoArchivo], { type: "text/plain" });var link = document.createElement("a");link.href = window.URL.createObjectURL(blob);link.download = "data.txt";link.click();} else {console.error("Error al obtener el archivo. Código de estado:", respuesta.status);}} catch (error) {console.error("Error en la solicitud:", error);}};
Edit: Tambien da problemas al obtener el archivo de sp