This commit is contained in:
tonyrewin 2023-01-28 14:00:06 +03:00
parent d2100cc865
commit a13b2a57a1
2 changed files with 26 additions and 24 deletions

View File

@ -58,25 +58,27 @@ def upload_storj(filecontent, filename, bucket_name):
@app.route('/api/upload', methods=['post'])
def upload():
print(request.files)
print(request.files.to_dict())
# check if the post request has the file part
if 'file' not in request.files:
return {'error': 'No file part'}, 400
file = request.files.get('file')
if 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
# submit a empty part without filename
if file.name == '':
if not filename:
return {'error': 'No selected file'}, 400
else:
# Save the file to a temporary location
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = os.path.join(temp_dir, filename)
file.save(temp_path)
# Open the file in binary mode
with open(temp_path, 'rb') as filecontent:
result = upload_storj(filecontent, filename, 'discoursio')
# Save the file to a temporary location
with tempfile.TemporaryDirectory() as temp_dir:
temp_path = os.path.join(temp_dir, filename)
file.save(temp_path)
# Open the file in binary mode
with open(temp_path, 'rb') as filecontent:
result = upload_storj(filecontent, filename, 'discoursio')
else:
return {'error': 'No selected file'}, 400
return {'message': 'File uploaded', 'result': jsonify(result)}, 200

View File

@ -28,26 +28,25 @@ export const ProfileSettingsPage = (props: PageProps) => {
}
let userpicFile: HTMLInputElement
const handleFileUpload = async (file: File) => {
try {
const formData = new FormData()
formData.append('file', file)
const response = await fetch('/api/upload', {
method: 'POST',
body: formData,
headers: {
'Content-Type': 'multipart/form-data; boundary=discoursiofile'
}
})
console.debug(response)
} catch (error) {
console.error('[upload] error', error)
}
const formData = new FormData()
formData.append('file', file)
console.log(formData)
const response = await fetch('/api/upload', {
method: 'POST',
body: formData,
headers: {
'Content-Type': 'multipart/form-data'
}
})
const json = await response.json()
console.debug(json)
}
const handleUserpicUpload = async (ev) => {
// TODO: show progress
console.debug('handleUserpicUpload')
try {
const f = ev.target.files[0]
if (f) handleFileUpload(f)
if (f) await handleFileUpload(f)
} catch (error) {
console.error('[upload] error', error)
}
@ -84,6 +83,7 @@ export const ProfileSettingsPage = (props: PageProps) => {
ref={userpicFile}
type="file"
name="file"
value="file"
hidden
onChange={handleUserpicUpload}
/>