Fix download: fetch file with auth header, create blob for proper download
This commit is contained in:
@@ -1,9 +1,11 @@
|
|||||||
import 'dart:ui';
|
import 'dart:ui';
|
||||||
|
import 'dart:js_interop';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||||
import 'package:file_picker/file_picker.dart' hide FileType;
|
import 'package:file_picker/file_picker.dart' hide FileType;
|
||||||
import 'package:path/path.dart' as p;
|
import 'package:path/path.dart' as p;
|
||||||
import 'package:web/web.dart' as web;
|
import 'package:web/web.dart' as web;
|
||||||
|
import 'package:http/http.dart' as http;
|
||||||
import '../blocs/file_browser/file_browser_bloc.dart';
|
import '../blocs/file_browser/file_browser_bloc.dart';
|
||||||
import '../blocs/file_browser/file_browser_event.dart';
|
import '../blocs/file_browser/file_browser_event.dart';
|
||||||
import '../blocs/file_browser/file_browser_state.dart';
|
import '../blocs/file_browser/file_browser_state.dart';
|
||||||
@@ -303,14 +305,14 @@ class _FileExplorerState extends State<FileExplorer> {
|
|||||||
const SizedBox(width: 12),
|
const SizedBox(width: 12),
|
||||||
Expanded(
|
Expanded(
|
||||||
child: Text(
|
child: Text(
|
||||||
'Preparing download: ${file.name}',
|
'Downloading: ${file.name}',
|
||||||
overflow: TextOverflow.ellipsis,
|
overflow: TextOverflow.ellipsis,
|
||||||
style: const TextStyle(color: AppTheme.primaryText),
|
style: const TextStyle(color: AppTheme.primaryText),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
],
|
],
|
||||||
),
|
),
|
||||||
duration: const Duration(seconds: 3),
|
duration: const Duration(days: 1), // Keep showing until dismissed
|
||||||
backgroundColor: AppTheme.primaryBackground,
|
backgroundColor: AppTheme.primaryBackground,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
@@ -335,16 +337,39 @@ class _FileExplorerState extends State<FileExplorer> {
|
|||||||
file.path,
|
file.path,
|
||||||
);
|
);
|
||||||
|
|
||||||
// Build full URL with token for authentication (anchor elements can't send headers)
|
// Build full URL
|
||||||
final fullUrl =
|
final fullUrl = '${fileService.baseUrl}$downloadUrl';
|
||||||
'${fileService.baseUrl}$downloadUrl&token=${Uri.encodeComponent(token)}';
|
|
||||||
|
|
||||||
// Trigger download via anchor element
|
// Fetch file with authentication header
|
||||||
|
final response = await http.get(
|
||||||
|
Uri.parse(fullUrl),
|
||||||
|
headers: {'Authorization': 'Bearer $token'},
|
||||||
|
);
|
||||||
|
|
||||||
|
if (response.statusCode != 200) {
|
||||||
|
throw Exception('Failed to download: ${response.statusCode}');
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create blob and trigger download
|
||||||
|
final bytes = response.bodyBytes;
|
||||||
|
final blob = web.Blob(
|
||||||
|
[bytes.toJS].toJS,
|
||||||
|
web.BlobPropertyBag(type: response.headers['content-type'] ?? 'application/octet-stream'),
|
||||||
|
);
|
||||||
|
final blobUrl = web.URL.createObjectURL(blob);
|
||||||
|
|
||||||
|
// Create anchor and trigger download
|
||||||
final anchor = web.HTMLAnchorElement()
|
final anchor = web.HTMLAnchorElement()
|
||||||
..href = fullUrl
|
..href = blobUrl
|
||||||
..download = file.name;
|
..download = file.name
|
||||||
|
..style.display = 'none';
|
||||||
|
web.document.body?.appendChild(anchor);
|
||||||
anchor.click();
|
anchor.click();
|
||||||
|
|
||||||
|
// Cleanup
|
||||||
|
web.document.body?.removeChild(anchor);
|
||||||
|
web.URL.revokeObjectURL(blobUrl);
|
||||||
|
|
||||||
// Dismiss preparing snackbar and show success
|
// Dismiss preparing snackbar and show success
|
||||||
snackController?.close();
|
snackController?.close();
|
||||||
if (context.mounted) {
|
if (context.mounted) {
|
||||||
|
|||||||
Reference in New Issue
Block a user