I want to create a file directory for my Flutter mobile application and upload files (any kind of images or pdf files).
I use the following code for selecting and uploading the file to the server :
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:file_picker/file_picker.dart';
class fileUpload extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: UploadScreen(),
);
}
}
class UploadScreen extends StatefulWidget {
@override
_UploadScreenState createState() => _UploadScreenState();
}
class _UploadScreenState extends State<UploadScreen> {
File? _selectedFile;
Future<void> _selectFile() async {
FilePickerResult? result = await FilePicker.platform.pickFiles();
if (result != null) {
setState(() {
_selectedFile = File(result.files.single.path!);
});
}
}
Future<void> _uploadFile() async {
if (_selectedFile == null) {
print('no files picked');
return;
}
var uri = Uri.parse('http://myhostingdomain.com/specialdirectory'); // Sunucunun URL'si
var request = http.MultipartRequest('POST', uri);
request.files.add(await http.MultipartFile.fromPath('file', _selectedFile!.path));
var response = await request.send();
if (response.statusCode == 200) {
print('successfully uploaded.');
} else {
print('Error returned: ${response.reasonPhrase}');
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('Sample file upload page'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
ElevatedButton(
onPressed: _selectFile,
child: Text('Choose a file'),
),
SizedBox(height: 20),
_selectedFile != null
? Text('selected file: ${_selectedFile!.path}')
: Text('no files found'),
SizedBox(height: 20),
ElevatedButton(
onPressed: _uploadFile,
child: Text('upload the file'),
),
],
),
),
);
}
}
When I run my mobile application everything looks file and file seems uploaded. But when I check the file server at the hosting destination, there is no file uploaded.
I believe I have to configure the public hosting server and send the authentication before I start the uploading the file.
How should I update my code to add the credentials while connecting the destination and additionally how should I configure the hosting server to accept uploadings?
Thank you
there are several solutions I found over internet but not including the credentials for a public hosting server.
2
Answers
I have noticed that writing a file (by uploading) directly to a remote server requires a lot of security constraints. If we remove the security constraints the zone (target directory) at the remote server becomes weak. That's why many solutions offer creating an interface to upload a file to the server. Such as a web page or webapi.
I have created the following webapi app in python to get the file and write to the server.
then I call this api from my flutter application :
I tested this code above and successfully worked for both local and remote (hosting) servers.
You have to be careful for the following points :
1- the webapi is hosted and running at the remote server. Python webapi using flask, must be run at Apache or NGINX, doesn't work on IIS directly.
2- the directory (e.g. /receivedfiles ) must be accessbile for the webapi application at the remote server.
3- the file type and file size must be configured at the flutter (dart) code before sending. If you remove the constraints (limits), you are most likely receive weird error messages that you can see at server logs.
Uploading a file is a multi-step process that requires work on the client and server side:
Server Side:
Create a folder to save your uploaded files. Make sure the folder is in your website directory and you can call it something like: UploadsFolder
For this example, I am using localhost.
Write the following code into a php file and call it uploadData.php
Client side:
write the following commands in your terminal to install the http and file_picker packages in flutter:
flutter pub add http
flutter pub add file_picker
Use the following code to upload your file