File Uploads

Last Edit: Oct 19 2020

The channel.sendImage and channel.sendFile methods make it easy to upload files.

This functionality defaults to using the Stream CDN. If you would like, you can easily change the logic to upload to your own CDN of choice.
The maximum file size is 20mb for the Stream Chat CDN.

const promises = [
    channel.sendImage(
        fs.createReadStream('./helloworld.jpg'),
        'hello_world1.jpg',
    ),
    channel.sendImage(
        fs.createReadStream('./helloworld.jpg'),
        'hello_world2.jpg',
    ),
];

const results = await Promise.all(promises);

const attachments = results.map(response => {
    return {
        type: 'image',
        thumb_url: response.file,
        asset_url: response.file,
    };
});

const response = await channel.sendMessage({
    text: 'Check out what I have uploaded in parallel',
    attachments,
});

expect(response.message.attachments).to.equal(attachments);
                    

File imageFile = new File("path");
File anyOtherFile = new File("path");

channelController.sendImage(imageFile, new ProgressCallback() {
    @Override
    public void onSuccess(@NotNull String file) {

    }

    @Override
    public void onError(@NotNull ChatError error) {

    }

    @Override
    public void onProgress(long progress) {

    }
});

channelController.sendFile(anyOtherFile, new ProgressCallback() {
    @Override
    public void onSuccess(@NotNull String file) {

    }

    @Override
    public void onError(@NotNull ChatError error) {

    }

    @Override
    public void onProgress(long progress) {

    }
});
                    

let channel = Client.shared.channel(type: .messaging, id: "general")
// Upload UIImage.
let image = UIImage(systemName: "circle")!
if let imageData = image.jpegData(compressionQuality: 0.9) {
    channel.sendImage(fileName: "Circle",
                      mimeType: AttachmentFileType.jpeg.mimeType, // or you can pass your own mimeType as string
                      imageData: imageData,
                      { (progress) in
                          // handle progress, eg update progressBar
                      }, { (result) in
                          // handle result
                      })
}
// Upload a local file with URL.
let url = Bundle.main.url(forResource: "calculations", withExtension: "pdf")!
if let fileData = try? Data(contentsOf: url) {
    channel.sendFile(fileName: "Calculations",
                     mimeType: AttachmentFileType.generic.mimeType, // or your own mimeType as string
                     fileData: fileData,
                     { (progress) in
                        // handle progress, eg update progressBar
                     }, { (result) in
                        // handle result
                     })
}

// Keep in mind that these functions will not make the attachments appear as visible in the channels.
// The attachment will be uploaded but will be invisible. To show it, you need to attach it
// to a message and send the message.
/*
Here are the steps to send an attachment to a channel:
1. As shown here, upload the image data using `channel.sendImage` (or channel.sendFile) and get the resulting URL.
2. Create the Attachment data as follows:
```
// for images
let attachment = Attachment(type: .image, title: "<#Your file name here#>, imageURL: <#The URL you get from sendImage#>)
// for files
let attachment = Attachment(type: .file, title: "<#Your file name here#>, url: <#The URL you get from sendFile#>
```
3. Create the Message object with this attachment:
```
let message = Message(text: "<#Message Body here#>", attachments: [attachment])
```
4. Send the message:
```
channel.send(message: message)
```
*/
                    

val imageFile = File("path")
val anyOtherFile = File("path")

// upload an image
channelController.sendImage(imageFile, object: ProgressCallback{
    override fun onSuccess(file: String) {

    }

    override fun onError(error: ChatError) {

    }

    override fun onProgress(progress: Long) {

    }

})

// upload a file
channelController.sendFile(anyOtherFile, object: ProgressCallback{
    override fun onSuccess(file: String) {

    }

    override fun onError(error: ChatError) {

    }

    override fun onProgress(progress: Long) {

    }

})
                    

$user = [ 'id' => 'jenny' ];

$response = $channel->sendImage('http://bit.ly/2O35mws', 'image.jpeg', $user);
                    

In the code example above, note how the message attachments are created after the files are uploaded. The React components support regular uploads, clipboard pasting, drag and drop, as well as URL enrichment via built-in open-graph scraping. As a bonus, the Stream CDN will automatically handle image resizing for you.