HELP! dfx cmd call with txt file

I have a txt file, and i want to pass that text from file to dfx command. How can i do that? Please help me with the syntax.
say text file : base64.txt and
DFX cmd : dfx canister call token setLogo ‘(“Text from text file , i want to pass”)’ .

I’m not sure if it works, maybe dfx deploy --argument=‘(“base64.txt”)’

Hello,

I have done something similar with a blob but the idea is the same with text.

Basically:

  1. I create a temporary file which will be the content of the parameters for the dfx command

  2. I run dfx canister call with --argument-file $TMP_FILE

I copy my script as an example:

#!/bin/sh
# Allow to upload binary to blobstore using blob type parameters
# Command sample
# dfx canister call corp_site replaceBlobRessource '(record {scheme="blob"; path="/header/test"; fragment=null; param=null},blob "\00\01\02\FF",null)' 
# dfx canister call corp_site replaceBlobRessource '(record {scheme="blob"; path="/test/b2"; fragment=null; param=null},vec{0;1;2;0xFF},null)'
# 
# parameters:
# $1 = the uri as record
# $2 = the binary file
# 
# Example:
# ./scripts/file2BlobStore.sh 'record {scheme="png"; path="/header/logo128"; fragment=null; param=null}' '/home/willy/dev/Alternance-Theatre/corp_site/src/corp_site_assets/assets/logo128.png'

status=$((0)) 

TMP_FILE=$(mktemp)

# Create beginning parameters
echo -n "( $1,blob \"" > $TMP_FILE

# Read file and create a string \xx\yy
for c in $(xxd -c 1 -p $2)
do 
  echo -n "\\$c" >> $TMP_FILE
done;

# Close parameters
echo -n "\",null)"  >> $TMP_FILE

echo "Parameter length: $(stat -c %s $TMP_FILE)"

# Call and store data
dfx canister call --argument-file $TMP_FILE corp_site addBlobRessource 

status=$((status + $?))

rm $TMP_FILE

exit $((status + $?))
1 Like

A feature was added to DFX 0.11.0 that enables reading a canister argument from a file. If your file contains the full canister argument, you can use the --argument-file parameter to read directly from it. If the provided file is -, then it will be instead read from stdin, so if it doesn’t have your (""), you can arrange it with cat or similar.

2 Likes