coollsd commited on
Commit
1ad2c1b
·
verified ·
1 Parent(s): 0e07159

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -13
app.py CHANGED
@@ -483,21 +483,35 @@ HTML_CONTENT = """
483
 
484
  while (retryCount < maxRetries) {
485
  try {
486
- const response = await fetch('/upload', {
487
- method: 'POST',
488
- body: formData
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
489
  });
490
 
491
- if (!response.ok) {
492
- throw new Error(`HTTP error! status: ${response.status}`);
493
- }
494
-
495
- const result = await response.json();
496
- if (result.url) {
497
- addResultLink(result.url, file.name);
498
- } else {
499
- alert('Upload failed: ' + result.error);
500
- }
501
  break; // Success, exit the loop
502
  } catch (error) {
503
  console.error('Upload error:', error);
 
483
 
484
  while (retryCount < maxRetries) {
485
  try {
486
+ const xhr = new XMLHttpRequest();
487
+ xhr.open('POST', '/upload', true);
488
+ xhr.upload.onprogress = (event) => updateProgress(event, progressBar.querySelector('.progress'));
489
+
490
+ xhr.onload = function() {
491
+ if (xhr.status === 200) {
492
+ const response = JSON.parse(xhr.responseText);
493
+ if (response.url) {
494
+ addResultLink(response.url, file.name);
495
+ } else {
496
+ alert('Upload failed: ' + response.error);
497
+ }
498
+ } else {
499
+ throw new Error(`HTTP error! status: ${xhr.status}`);
500
+ }
501
+ };
502
+
503
+ xhr.onerror = function() {
504
+ throw new Error('Network error occurred');
505
+ };
506
+
507
+ xhr.send(formData);
508
+
509
+ // Wait for the request to complete
510
+ await new Promise((resolve, reject) => {
511
+ xhr.onloadend = resolve;
512
+ xhr.onerror = reject;
513
  });
514
 
 
 
 
 
 
 
 
 
 
 
515
  break; // Success, exit the loop
516
  } catch (error) {
517
  console.error('Upload error:', error);