Back

How to upload the file in cloud function

  • 0
  • Flutter
  • Functions
  • Cloud
Mosh Ontong
30 Sep, 2023, 14:19

I want to upload some file from client side into the server cloud function. I want to bring that file in my appwrite function, How to do this?

TL;DR
The user wants to know how to upload a file in a cloud function. One person suggests using the 'multipartfile' method. Another person provides a code example using Appwrite AI. The code example shows how to send a file to the cloud function using the http package in Dart. The code example includes adding the file to the request and handling the response. The user asks for validation of the code. There is no solution provided in the thread.
safwan
30 Sep, 2023, 14:22

You should be able to use the platform specific approach to send a file in a request. I haven’t had to do this yet, so not sure

Mosh Ontong
30 Sep, 2023, 14:23

currently I am using flutter

safwan
30 Sep, 2023, 14:23

From the top of my head, you could convert the image into a base64 string, and send it to the server where you can decode it

Mosh Ontong
30 Sep, 2023, 14:23

ohh I seee

Mosh Ontong
30 Sep, 2023, 14:27

This is the answer of Appwrite AI:

TypeScript
import 'package:http/http.dart' as http;
import 'dart:io';

void sendFileToCloudFunction(File file) async {
  var request = http.MultipartRequest(
    'POST',
    Uri.parse('YOUR_CLOUD_FUNCTION_ENDPOINT'),
  );
  
  // Add the file to the request
  var fileStream = http.ByteStream(file.openRead());
  var length = await file.length();
  var multipartFile = http.MultipartFile('file', fileStream, length,
      filename: file.path.split('/').last);
  
  request.files.add(multipartFile);
  
  // Send the request
  var response = await request.send();
  
  // Handle the response
  if (response.statusCode == 200) {
    print('File uploaded successfully');
  } else {
    print('Error uploading file: ${response.reasonPhrase}');
  }
}

What do you think is this works?

PS: I need your help @Meldiron I just want to validate the code if valid

Drake
30 Sep, 2023, 15:50

If you're using the custom function endpoint, you can send a file just like you would any other API endpoint

Drake
30 Sep, 2023, 15:50

Yes, multipartfile is the way to go

Drake
30 Sep, 2023, 15:50

Please don't tag people like this as it can be very disruptive

Mosh Ontong
30 Sep, 2023, 16:11

sorry

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more