feat: added app routing
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import { getSP } from '../pnp/pnpjs';
|
||||
import '@pnp/sp/attachments';
|
||||
|
||||
export interface IAttachmentInfo {
|
||||
fileName: string;
|
||||
/** ServerRelativeUrl — прямая ссылка на скачивание. */
|
||||
url: string;
|
||||
}
|
||||
|
||||
/** Прочитать файл как ArrayBuffer (FileReader — совместимо со всеми браузерами). */
|
||||
function readAsArrayBuffer(file: File): Promise<ArrayBuffer> {
|
||||
return new Promise<ArrayBuffer>((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.onload = () => resolve(reader.result as ArrayBuffer);
|
||||
reader.onerror = () => reject(reader.error);
|
||||
reader.readAsArrayBuffer(file);
|
||||
});
|
||||
}
|
||||
|
||||
/** Вложения элемента списка (для режима согласующего — ссылки на скачивание). */
|
||||
export async function getAttachments(listTitle: string, itemId: number): Promise<IAttachmentInfo[]> {
|
||||
const files = await getSP()
|
||||
.web.lists.getByTitle(listTitle)
|
||||
.items.getById(itemId)
|
||||
.attachmentFiles();
|
||||
return files.map((f) => ({ fileName: f.FileName, url: f.ServerRelativeUrl }));
|
||||
}
|
||||
|
||||
/**
|
||||
* Прикрепить файлы к уже созданному элементу заявки.
|
||||
* Вызывать ПОСЛЕ создания элемента (нужен его id).
|
||||
*/
|
||||
export async function uploadAttachments(
|
||||
listTitle: string,
|
||||
itemId: number,
|
||||
files: File[]
|
||||
): Promise<void> {
|
||||
if (files.length === 0) {
|
||||
return;
|
||||
}
|
||||
const item = getSP().web.lists.getByTitle(listTitle).items.getById(itemId);
|
||||
for (let i = 0; i < files.length; i++) {
|
||||
const file = files[i];
|
||||
const content = await readAsArrayBuffer(file);
|
||||
await item.attachmentFiles.add(file.name, content);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user