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:
@@ -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)
|
||||
);
|
||||
|
||||
@@ -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
9
src/types/UploadFile.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
/**
|
||||
* Represents a object that is passed to file uploader
|
||||
*
|
||||
*/
|
||||
export interface UploadFile {
|
||||
file: File;
|
||||
fileName: string;
|
||||
}
|
||||
|
||||
@@ -8,3 +8,4 @@ export * from "./SASjsRequest";
|
||||
export * from "./SASjsWaitingRequest";
|
||||
export * from "./ServerType";
|
||||
export * from "./Session";
|
||||
export * from "./UploadFile";
|
||||
|
||||
Reference in New Issue
Block a user