1
0
mirror of https://github.com/sasjs/adapter.git synced 2026-01-03 18:50:05 +00:00

fix: uploadFile accept multiple files

This commit is contained in:
Mihajlo Medjedovic
2020-07-28 13:23:57 +02:00
parent 2ce3669b10
commit 261913d2d7
29 changed files with 3193 additions and 28 deletions

View File

@@ -1,5 +1,6 @@
import { isLogInRequired, needsRetry } from "./utils";
import { CsrfToken } from "./types/CsrfToken";
import { UploadFile } from "./types/UploadFile";
const requestRetryLimit = 5;
@@ -12,9 +13,8 @@ export class FileUploader {
) {}
private retryCount = 0;
public uploadFile(sasJob: string, file: File, fileName: string, params: any) {
if (!file) throw new Error("File must be provided");
if (!fileName) throw new Error("File name must be provided");
public uploadFile(sasJob: string, files: UploadFile[], params: any) {
if (files?.length < 1) throw new Error("Atleast one file must be provided");
let paramsString = "";
@@ -38,8 +38,10 @@ export class FileUploader {
return new Promise((resolve, reject) => {
const formData = new FormData();
formData.append("file", file);
formData.append("filename", fileName);
for (let file of files) {
formData.append("file", file.file, file.fileName);
}
if (this.csrfToken) formData.append("_csrf", this.csrfToken.value);
fetch(uploadUrl, {
@@ -72,7 +74,7 @@ export class FileUploader {
if (needsRetry(responseText)) {
if (this.retryCount < requestRetryLimit) {
this.retryCount++;
this.uploadFile(sasJob, file, fileName, params).then(
this.uploadFile(sasJob, files, params).then(
(res: any) => resolve(res),
(err: any) => reject(err)
);

View File

@@ -21,6 +21,7 @@ import {
SASjsWaitingRequest,
ServerType,
CsrfToken,
UploadFile
} from "./types";
import { SASViyaApiClient } from "./SASViyaApiClient";
import { SAS9ApiClient } from "./SAS9ApiClient";
@@ -379,11 +380,10 @@ export default class SASjs {
* @param sasJob - The path to the SAS program (ultimately resolves to
* the SAS `_program` parameter to run a Job Definition or SAS 9 Stored
* Process.) Is prepended at runtime with the value of `appLoc`.
* @param file - File to be uploaded
* @param fileName - Name of the file to be uploaded
* @param file - Array of files to be uploaded, including File object and file name.
* @param params - Request URL paramaters
*/
public uploadFile(sasJob: string, file: File, fileName: string, params: any) {
public uploadFile(sasJob: string, files: UploadFile[], fileName: string, params: any) {
const fileUploader =
this.fileUploader ||
new FileUploader(
@@ -392,7 +392,7 @@ export default class SASjs {
this.jobsPath,
this.csrfTokenWeb
);
return fileUploader.uploadFile(sasJob, file, fileName, params);
return fileUploader.uploadFile(sasJob, files, params);
}
/**

9
src/types/UploadFile.ts Normal file
View File

@@ -0,0 +1,9 @@
/**
* Represents a object that is passed to file uploader
*
*/
export interface UploadFile {
file: File;
fileName: string;
}

View File

@@ -8,3 +8,4 @@ export * from "./SASjsRequest";
export * from "./SASjsWaitingRequest";
export * from "./ServerType";
export * from "./Session";
export * from "./UploadFile";