Image Uploading to Traccar Server

Furqan Mughal25 days ago

Hey, I want to add feature for user's to upload image in our app. We want to know how Traccar Server handle's media (image) uploading to it's own server so that i save uploaded image url into my user's -> attribute param.
I have seen previous image related questions but can't find solution or answer.
Help me out!
Thanks in advance.

Furqan Mughal25 days ago

Post Api - URL: https://x912408e9.traccar.com/api/media
Code:

Future<String?> uploadImageToTraccar({
    required File file,
    required String baseUrl,
    required String sessionId,
  }) async {
    try {
      var uri = Uri.parse("$baseUrl/api/media");

      var request = http.MultipartRequest("POST", uri)
        ..headers['Cookie'] = "JSESSIONID=$sessionId"
        ..files.add(await http.MultipartFile.fromPath("image_name", file.path));

      var response = await request.send();

      if (response.statusCode == 200) {
        var respStr = await response.stream.bytesToString();
        var jsonResp = jsonDecode(respStr);

        var mediaId = jsonResp['id'];
        if (mediaId != null) {
          return "$baseUrl/api/media/$mediaId/image_name";
        }
      } else {
        print("Upload failed: ${response.statusCode}");
      }
    } catch (e) {
      print("Error uploading image: $e");
    }
    return null;
  }
Furqan Mughal19 days ago

Anyone for Help ???

Anton Tananaev19 days ago

You should compare your requests to what the official web app sends.

Furqan Mughal3 days ago

We can upload image or not ?

Anton Tananaev3 days ago

Yes, you can.

Furqan Mughal3 days ago

Thanks,
If i am handling a separate list under users-> attributes as json string.
I want to add image for each list item on Traccar Server and then save my list as json into attributes.
Can we do that too?
I am just asking if it is possible or not?

Anton Tananaev3 days ago

You can't upload to user attributes. That doesn't make sense. You can upload an image in general and then add a URL to the attributes.

Furqan Mughal2 days ago

OK Thanks for your help!