captl
This commit is contained in:
parent
d2100cc865
commit
a13b2a57a1
|
@ -58,25 +58,27 @@ def upload_storj(filecontent, filename, bucket_name):
|
||||||
|
|
||||||
@app.route('/api/upload', methods=['post'])
|
@app.route('/api/upload', methods=['post'])
|
||||||
def upload():
|
def upload():
|
||||||
|
print(request.files)
|
||||||
|
print(request.files.to_dict())
|
||||||
# check if the post request has the file part
|
# check if the post request has the file part
|
||||||
if 'file' not in request.files:
|
if 'file' not in request.files:
|
||||||
return {'error': 'No file part'}, 400
|
return {'error': 'No file part'}, 400
|
||||||
file = request.files.get('file')
|
file = request.files.get('file')
|
||||||
if file:
|
if file:
|
||||||
# save the file
|
# save the file
|
||||||
filename = secure_filename(file.name)
|
filename = secure_filename(file.name or file.filename)
|
||||||
# if user does not select file, browser also
|
# if user does not select file, browser also
|
||||||
# submit a empty part without filename
|
# submit a empty part without filename
|
||||||
if file.name == '':
|
if not filename:
|
||||||
return {'error': 'No selected file'}, 400
|
return {'error': 'No selected file'}, 400
|
||||||
else:
|
else:
|
||||||
# Save the file to a temporary location
|
# Save the file to a temporary location
|
||||||
with tempfile.TemporaryDirectory() as temp_dir:
|
with tempfile.TemporaryDirectory() as temp_dir:
|
||||||
temp_path = os.path.join(temp_dir, filename)
|
temp_path = os.path.join(temp_dir, filename)
|
||||||
file.save(temp_path)
|
file.save(temp_path)
|
||||||
# Open the file in binary mode
|
# Open the file in binary mode
|
||||||
with open(temp_path, 'rb') as filecontent:
|
with open(temp_path, 'rb') as filecontent:
|
||||||
result = upload_storj(filecontent, filename, 'discoursio')
|
result = upload_storj(filecontent, filename, 'discoursio')
|
||||||
else:
|
else:
|
||||||
return {'error': 'No selected file'}, 400
|
return {'error': 'No selected file'}, 400
|
||||||
return {'message': 'File uploaded', 'result': jsonify(result)}, 200
|
return {'message': 'File uploaded', 'result': jsonify(result)}, 200
|
||||||
|
|
|
@ -28,26 +28,25 @@ export const ProfileSettingsPage = (props: PageProps) => {
|
||||||
}
|
}
|
||||||
let userpicFile: HTMLInputElement
|
let userpicFile: HTMLInputElement
|
||||||
const handleFileUpload = async (file: File) => {
|
const handleFileUpload = async (file: File) => {
|
||||||
try {
|
const formData = new FormData()
|
||||||
const formData = new FormData()
|
formData.append('file', file)
|
||||||
formData.append('file', file)
|
console.log(formData)
|
||||||
const response = await fetch('/api/upload', {
|
const response = await fetch('/api/upload', {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
body: formData,
|
body: formData,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'multipart/form-data; boundary=discoursiofile'
|
'Content-Type': 'multipart/form-data'
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
console.debug(response)
|
const json = await response.json()
|
||||||
} catch (error) {
|
console.debug(json)
|
||||||
console.error('[upload] error', error)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
const handleUserpicUpload = async (ev) => {
|
const handleUserpicUpload = async (ev) => {
|
||||||
// TODO: show progress
|
// TODO: show progress
|
||||||
|
console.debug('handleUserpicUpload')
|
||||||
try {
|
try {
|
||||||
const f = ev.target.files[0]
|
const f = ev.target.files[0]
|
||||||
if (f) handleFileUpload(f)
|
if (f) await handleFileUpload(f)
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('[upload] error', error)
|
console.error('[upload] error', error)
|
||||||
}
|
}
|
||||||
|
@ -84,6 +83,7 @@ export const ProfileSettingsPage = (props: PageProps) => {
|
||||||
ref={userpicFile}
|
ref={userpicFile}
|
||||||
type="file"
|
type="file"
|
||||||
name="file"
|
name="file"
|
||||||
|
value="file"
|
||||||
hidden
|
hidden
|
||||||
onChange={handleUserpicUpload}
|
onChange={handleUserpicUpload}
|
||||||
/>
|
/>
|
||||||
|
|
Loading…
Reference in New Issue
Block a user