본문으로 건너뛰기

서버에 업로드 ​

뷰어는 ‘적용’의 결과를 바로 보여 주지만, 저장은 앱이 할 일입니다. 이 예제는 편집한 이미지를 서버로 보내고 결과를 사용자에게 알립니다.

이 함수는 이미지를 폼의 파일로 보내고, 서버가 받지 않으면 실패합니다.

ts
async function upload(blob: Blob, type: string) {
  const body = new FormData()
  body.append('file', blob, `document.${type.split('/')[1]}`)
  const response = await fetch('/api/documents/42', { method: 'PUT', body })
  if (!response.ok) {
    throw new Error(`The upload failed with status ${response.status}`)
  }
}

뷰어가 적용할 때 이 함수를 호출하고, 상태 줄이 결과를 알려 줍니다.

html
<div id="viewer" style="height: 480px"></div>
<p id="status" role="status"></p>
ts
import { PaperPic } from '@paperpic/core'
import '@paperpic/core/style.css'

const status = document.querySelector('#status')!

const pic = new PaperPic(document.querySelector<HTMLElement>('#viewer')!, {
  src: '/documents/42.jpg',
  alt: '청구서 42',
  maxOutputSize: 3000,
})

pic.on('apply', async ({ blob, type }) => {
  status.textContent = '저장 중…'
  try {
    await upload(blob, type)
    status.textContent = '저장됨'
  }
  catch {
    status.textContent = '문서를 저장하지 못했습니다'
  }
})

참고 ​

  • role="status"는 스크린 리더가 업로드 결과를 읽어 주게 합니다.
  • maxOutputSize는 파일을 작게 유지합니다. 긴 변이 3000픽셀이어도 문서는 읽기 좋습니다.
  • 적용한 뒤에는 기록이 비므로, 사용자가 이전 이미지로 실행 취소할 수 없습니다. 다시 시도 버튼을 두려면 마지막 blob과 type을 보관했다가 upload를 다시 호출하세요.
  • 흑백 문서라면 서버는 PNG를 받고, 그렇지 않으면 원본이 JPEG, PNG, WebP일 때 그 형식을 받습니다. PaperPic이 형식을 판단하는 방법과 다른 형식을 고르는 방법은 결과 저장을 참고하세요.

MIT 라이선스로 배포됩니다.