mirror of
				https://github.com/langgenius/dify.git
				synced 2025-11-04 04:43:09 +00:00 
			
		
		
		
	
		
			
				
	
	
		
			37 lines
		
	
	
		
			913 B
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			37 lines
		
	
	
		
			913 B
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
import { upload } from '@/service/base'
 | 
						|
 | 
						|
type ImageUploadParams = {
 | 
						|
  file: File
 | 
						|
  onProgressCallback: (progress: number) => void
 | 
						|
  onSuccessCallback: (res: { id: string }) => void
 | 
						|
  onErrorCallback: () => void
 | 
						|
}
 | 
						|
type ImageUpload = (v: ImageUploadParams, isPublic?: boolean, url?: string) => void
 | 
						|
export const imageUpload: ImageUpload = ({
 | 
						|
  file,
 | 
						|
  onProgressCallback,
 | 
						|
  onSuccessCallback,
 | 
						|
  onErrorCallback,
 | 
						|
}, isPublic, url) => {
 | 
						|
  const formData = new FormData()
 | 
						|
  formData.append('file', file)
 | 
						|
  const onProgress = (e: ProgressEvent) => {
 | 
						|
    if (e.lengthComputable) {
 | 
						|
      const percent = Math.floor(e.loaded / e.total * 100)
 | 
						|
      onProgressCallback(percent)
 | 
						|
    }
 | 
						|
  }
 | 
						|
 | 
						|
  upload({
 | 
						|
    xhr: new XMLHttpRequest(),
 | 
						|
    data: formData,
 | 
						|
    onprogress: onProgress,
 | 
						|
  }, isPublic, url)
 | 
						|
    .then((res: { id: string }) => {
 | 
						|
      onSuccessCallback(res)
 | 
						|
    })
 | 
						|
    .catch(() => {
 | 
						|
      onErrorCallback()
 | 
						|
    })
 | 
						|
}
 |