1
1
import React , { useState , useEffect } from "react" ;
2
2
import { SaveFileSize } from "../../constant/saveFileSize" ;
3
- // import Parse from "parse";
4
- // import sanitizeFileName from "../../primitives/sanitizeFileName";
5
- import axios from "axios" ;
3
+ import Parse from "parse" ;
4
+ import sanitizeFileName from "../../primitives/sanitizeFileName" ;
6
5
7
6
const FileUpload = ( props ) => {
8
7
const [ parseBaseUrl ] = useState ( localStorage . getItem ( "baseUrl" ) ) ;
@@ -69,53 +68,44 @@ const FileUpload = (props) => {
69
68
} ;
70
69
71
70
const handleFileUpload = async ( file ) => {
71
+ Parse . serverURL = parseBaseUrl ;
72
+ Parse . initialize ( parseAppId ) ;
72
73
setfileload ( true ) ;
73
- const file_url = parseBaseUrl . slice ( 0 , - 4 ) ;
74
- const url = `${ file_url } file_upload` ;
75
- const formData = new FormData ( ) ;
76
- formData . append ( "file" , file ) ;
77
- const config = {
78
- headers : {
79
- "content-type" : "multipart/form-data" ,
80
- "X-Parse-Application-Id" : parseAppId
81
- } ,
82
- onUploadProgress : function ( progressEvent ) {
83
- var percentCompleted = Math . round (
84
- ( progressEvent . loaded * 100 ) / progressEvent . total
85
- ) ;
86
- setpercentage ( percentCompleted ) ;
87
- }
88
- } ;
74
+ const size = file . size ;
75
+ const fileName = file . name ;
76
+ const name = sanitizeFileName ( fileName ) ;
77
+ const pdfFile = file ;
78
+ const parseFile = new Parse . File ( name , pdfFile ) ;
89
79
90
80
try {
91
- await axios
92
- . post ( url , formData , config )
93
- . then ( ( res ) => {
94
- if ( res . data . status === "Error" ) {
95
- alert ( res . data . message ) ;
81
+ const response = await parseFile . save ( {
82
+ progress : ( progressValue , loaded , total , { type } ) => {
83
+ if ( type === "upload" && progressValue !== null ) {
84
+ const percentCompleted = Math . round ( ( loaded * 100 ) / total ) ;
85
+ console . log ( "percentCompleted " , percentCompleted ) ;
86
+ setpercentage ( percentCompleted ) ;
96
87
}
97
- setFileUpload ( res . data . imageUrl ) ;
98
- props . onChange ( res . data . imageUrl ) ;
99
- setfileload ( false ) ;
100
- setpercentage ( 0 ) ;
88
+ }
89
+ } ) ;
101
90
102
- if ( res . data . imageUrl ) {
103
- SaveFileSize ( file . size , res . data . imageUrl ) ;
104
- return res . data . imageUrl ;
105
- }
106
- } )
107
- . catch ( ( err ) => {
108
- alert ( `${ err . message } ` ) ;
109
- setfileload ( false ) ;
110
- setpercentage ( 0 ) ;
111
- } ) ;
91
+ setFileUpload ( response . url ( ) ) ;
92
+ props . onChange ( response . url ( ) ) ;
93
+ setfileload ( false ) ;
94
+ // The response object will contain information about the uploaded file
95
+ // console.log("File uploaded:", response);
96
+
97
+ // You can access the URL of the uploaded file using response.url()
98
+ // console.log("File URL:", response.url());
99
+ if ( response . url ( ) ) {
100
+ SaveFileSize ( size , response . url ( ) ) ;
101
+ return response . url ( ) ;
102
+ }
112
103
} catch ( error ) {
113
- alert ( error . message ) ;
114
104
setfileload ( false ) ;
115
105
setpercentage ( 0 ) ;
106
+ console . error ( "Error uploading file:" , error ) ;
116
107
}
117
108
} ;
118
-
119
109
let fileView =
120
110
props . formData &&
121
111
props . schema . uploadtype === "s3viajw" ? null : props . formData &&
@@ -124,7 +114,7 @@ const FileUpload = (props) => {
124
114
< a
125
115
href = { props . formData }
126
116
title = { props . formData }
127
- style = { { paddingBottom : "10px" , color : "blue" } }
117
+ style = { { paddingBottom : "10px" , color : "blue" } }
128
118
>
129
119
Download
130
120
</ a >
0 commit comments