Skip to content

serverpod file upload  #2562

Closed
Closed

Activity

  1. SandPod commented on Aug 5, 2024

    @SandPod
    Contributor

    Hey @hidayet-poscu 👋

    Did you follow the steps in the documentation? https://docs.serverpod.dev/concepts/file-uploads
    More specifically the steps on the client: https://docs.serverpod.dev/concepts/file-uploads#client-side-code

    How does your code for uploading look?
    Is the file actually uploaded to the server?

    The file upload does not work with Serverpod mini since it requires a database.

  2. added
    in triage
    on Aug 5, 2024
  3. xyzbilal commented on Aug 5, 2024

    @xyzbilal

    Hey @SandPod I also have similar problem with getting public url or we do not understand exactly how to implement the logic.

    this function is endpoint function to store files in a folder and it creates folder an file under server folder with no problem.

    Future<String?> storeFile(Session session,{
      
        required String folder,
        required String filename,
        required ByteData byteData,
        DateTime? expiration,
        bool verified = true,
      }) async {
       
        try {
          var fullPath = 'uploads/$folder/$filename';
    
          print(fullPath);
          final buffer = byteData.buffer;
          final file =  File(fullPath);
    
          await file.create(recursive: true);
          
          file.writeAsBytes(
    
          buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes));
          final url = await getPublicUrl(session, fullPath);
          return url.toString();
        
        } catch (e) {
          throw Exception('Failed to store file. ($e)');
        }
      }
    
    

    also retrieving file from client side is also with this function is also has no proplem

    endpoint function

      Future<ByteData?> retrieveFile(Session session,{
    
        required String path,
      }) async {
    
        try {
          
          final file =File("uploads/$path");
    
    
          if (await file.exists()) {
            print("File exists");
            final ff = await file.readAsBytes();
            final f = ByteData.view(ff.buffer);
            return f;
    
          }
        } catch (e) {
          throw Exception('Failed to retrieve file. ($e)');
        }
    
        return null;
      }
    

    in client side calling retrieveFile() function works fine downloading file as ByteData ane converting it to file is also ok.

    The problem is when we try to make url public and try to get or show file directly using url I cant success.
    I tried http://localhost:8080/uploads/file.pdf or http://localhost:8080/uploads/previews/file.png which are where I save files also is not useful for the purpose.
    Thanks for any kind of info.

    Edit : I wonder if my issue and the question are different issues

  4. SandPod commented on Aug 5, 2024

    @SandPod
    Contributor

    Looking at your implementation, it does not seem like you followed the documentation.

    Try defining the two server endpoints in the documentation along with the suggested single client code.

    The local file upload is implemented using the database. So if the files have not been uploaded and correctly registered in the database the server won't be able to find it.

    The session.storage.createDirectFileUploadDescription method ensures that the database is ready for the file upload. And

    var uploader = FileUploader(uploadDescription);
    await uploader.upload(myStream);

    ensures that the file is correctly uploaded to the server and is ready to be served.

    Give this a go and let me know if you run into any more issues.

  5. xyzbilal commented on Aug 6, 2024

    @xyzbilal

    my problem was saving files to local directory of server instead of database. I created own files to solve the problem and its working fine.

    I also created PR for it if someone else needs it. PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in triage

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions