Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File attachments with non-ASCII filenames become garbled when uploaded via node-asana to API #298

Open
dranitski opened this issue Dec 10, 2024 · 0 comments

Comments

@dranitski
Copy link

When uploading file attachments to Asana tasks that contain non-ASCII characters in filenames (e.g. Japanese, Cyrillic, Greek characters), the filenames become garbled/corrupted when viewed in Asana web interface.

Example:
image

The issue appears to be related to character encoding handling in the multipart form data upload. The same file uploads work correctly when using Postman, suggesting this is specific to node-asana http client implementation.

Original forum discussion:

https://forum.asana.com/t/attachment-names-uploaded-with-asana-api-are-garbled-on-asanaweb/286200

Expected behavior:

Filenames with non-ASCII characters should display correctly in Asana web interface after upload.
Character encoding should be preserved throughout the upload process

Current behavior:

Filenames with non-ASCII characters appear corrupted/garbled after upload
Japanese, Cyrillic, Greek and other non-ASCII characters are not handled properly

Impact:

This affects any applications using the Node.js client to upload files with non-ASCII filenames to Asana tasks, making the filenames unreadable.

Working axios code with proper encoding:

const axios = require('axios');
const fs = require('fs');
const path = require('path');
const mime = require('mime-types');
require('dotenv').config();

const uploadFileToAsana = async (filePath, parentId) => {
    const filename = path.basename(filePath);
    const mimeType = mime.lookup(filename) || 'application/octet-stream';

    // Generate boundary
    const boundary = '----WebKitFormBoundary' + Math.random().toString(36).slice(2);

    // Create form data manually
    const form = [];

    // Add file part
    form.push(Buffer.from(`--${boundary}\r\n`));
    form.push(Buffer.from(`Content-Disposition: form-data; name="file"; filename*=utf-8''${encodeURIComponent(filename)}\r\n`));
    form.push(Buffer.from(`Content-Type: ${mimeType}\r\n\r\n`));
    form.push(fs.readFileSync(filePath));
    form.push(Buffer.from('\r\n'));

    // Add parent part
    form.push(Buffer.from(`--${boundary}\r\n`));
    form.push(Buffer.from('Content-Disposition: form-data; name="parent"\r\n\r\n'));
    form.push(Buffer.from(`${parentId}\r\n`));
    form.push(Buffer.from(`--${boundary}--\r\n`));

    // Calculate content length
    const contentLength = form.reduce((acc, curr) => acc + curr.length, 0);

    try {
        const response = await axios({
            method: 'post',
            url: 'https://app.asana.com/api/1.0/attachments',
            headers: {
                'Authorization': `Bearer YOUR_ASANA_TOKEN`,
                'Accept': 'application/json',
                'Content-Type': `multipart/form-data; boundary=${boundary}`,
                'Content-Length': contentLength
            },
            data: Buffer.concat(form),
            maxBodyLength: Infinity,
            maxContentLength: Infinity,
            maxRedirects: 20
        });

        return response.data;
    } catch (error) {
        console.error('Upload failed:', error.response?.data || error.message);
        throw error;
    }
};

// Example usage
uploadFileToAsana('./файл.pdf', '1208902923833135')
    .then(result => console.log('Upload successful:', result))
    .catch(error => console.error('Upload failed:', error));

// module.exports = uploadFileToAsana;

Environment:

Package: asana (npm)
Version: 3.0.11
Node.js environment

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

No branches or pull requests

1 participant