# Structure FileTransferContext and file\_data

**URL:** <https://community.f5.com/t/structure-filetransfercontext-and-file-data/61991>\
**Category:** Technical Forums\
**Tags:** icontrol, dev, devops\
**Created:** [March 20, 2008, 12:49pm UTC](https://community.f5.com/t/structure-filetransfercontext-and-file-data/61991 "2008-03-20T12:49:36Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nicolas\_Menant\_1](https://avatars.discourse-cdn.com/v4/letter/n/f19dbf/32.png) [@Nicolas\_Menant\_1](https://community.f5.com/u/Nicolas_Menant_1)\
**Post date:** [March 20, 2008, 12:49pm UTC](https://community.f5.com/t/structure-filetransfercontext-and-file-data/61991/1 "2008-03-20T12:49:36Z")

</div>

Hi,

i try to create an windows API to retrieve an external class from a BIGIP, update it and then upload it again on the box.

I have something that seems strange:

in the SDK for the structure FileTransferContext it says that file\_data is of type char[]

or when i try to compile my code it says: cannot implicity convert type ‘string’ to ‘byte[]’.

When i did the download of the file i had to make an explicit cast (char) FTC.file\_data[j] to make it work.

Is it possible the SDK is wrong or am i missing something (which is quite possible since i have developped anything since a long long time)

Thanks for your help!

Here is part of my code:

```tcl
iControl.SystemConfigSyncFileTransferContext FTC = new iControl.SystemConfigSyncFileTransferContext();
int exit = 0;
int i, j;
string element;
       
for (i = 0; i < listBox_class.Items.Count; i++)
{
  element = "\""+listBox_class.Items[\i].ToString()+"\",\n";
  FTC.file_data = new byte[element.Length];
  for (j = 0; j < element.Length; j++)
    FTC.file_data[j] = (byte)((int)element[j]);
  if (j == 0)
    FTC.chain_type = iControl.CommonFileChainType.FILE_FIRST_AND_LAST;
  else if (j == (element.Length - 1))
    FTC.chain_type = iControl.CommonFileChainType.FILE_MIDDLE;
  else
    FTC.chain_type = iControl.CommonFileChainType.FILE_LAST;
  element = element.Remove(0);
  my_interface.SystemConfigSync.upload_file(filename, FTC);
}

```

Another question:

When i use upload\_file, it looks like it appends the new data to the existing file, is it possible to make it overwrite the previous file ? or do i have to delete the file before doing the upload ?

Thanks

---

<div class="post-metadata">

**Author:** ![Joe\_Pruitt](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/joe_pruitt/32/8438_2.png) [@Joe\_Pruitt](https://community.f5.com/u/Joe_Pruitt)\
**Post date:** [March 20, 2008, 7:48pm UTC](https://community.f5.com/t/structure-filetransfercontext-and-file-data/61991/2 "2008-03-20T19:48:07Z")

</div>

char[] is the happy medium I took on the SDK to cover all languages. Essentially it will be converted to a base64 string across the wire so for .NET that would be an array of bytes (byte[]).

There is a sample in the SDK for ConfigSync that illustrates most of the ConfigSync interfaces. Since your code looks like C, here’s the routine in ConfigSyncMain.cs file in the SDK’s ConfigSync sample:

```tcl
void handle_upload(string local_file, string config_name)
{
  ConfigSync.SystemConfigSyncFileTransferContext ctx = new ConfigSync.SystemConfigSyncFileTransferContext();
  bool bContinue = true;
  ctx.chain_type = CommonFileChainType.FILE_FIRST;
  long chunk_size = 1024;
  long total_bytes = 0;
  FileStream fs = new FileStream(local_file, FileMode.Open);
  BinaryReader r = new BinaryReader(fs);
  while (bContinue)
  {
    ctx.file_data = r.ReadBytes(Convert.ToInt32(chunk_size));
    if ( ctx.file_data.Length != chunk_size )
    {
      // At the end. Check to see if it is the first request also.
      if ( 0 == total_bytes )
      {
        ctx.chain_type = CommonFileChainType.FILE_FIRST_AND_LAST;
      }
      else
      {
        ctx.chain_type = CommonFileChainType.FILE_LAST;
      }
      bContinue = false;
    }
    total_bytes += ctx.file_data.Length;
    
    // Upload bytes.
    ConfigSync.upload_configuration(config_name, ctx);
    // Move to middle 
    ctx.chain_type = CommonFileChainType.FILE_MIDDLE;
    Console.WriteLine("Uploaded " + total_bytes + " bytes");
  }
}

```

Now, this uses the upload\_configuration() method, but the upload\_file() method works the same way. Also, this doesn’t do you much good if you want to use a string (as opposed to a file read with BinaryReader) as input. To get a byte array from a string, you’ll want to use the System.Text.ASCIIEncoding classes GetBytes() method. Here’s a wrapper function that converts a string into a byte array.

```tcl
public static byte[] StrToByteArray(string str)
{
  System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
  return encoding.GetBytes(str);
}

```

Also, this method shows how to “chunk” up the request’s for very large uploads. Feel free to change “chunk\_size” to something very large (or even the size of the input string) to make it all go in one shot.

To answer why your code always appends the data to the file, I don’t see where your first write is using the FILE\_FIRST or FILE\_FIRST\_OR\_LAST chain type. This will tell the server to truncate the existing file before the write of the data. If you have FILE\_MIDDLE or FILE\_LAST as the first write, it will append to the file.

If you want a simple function that takes input as a string and uploads the entire string in one shot, this should do it for you (making use of the above conversion method)

```tcl
void uploadStringToFile(String target_file, String contents)
{
  iControl.SystemConfigSyncFileTransferContext ctx = new iControl.SystemConfigSyncFileTransferContext();
  ctx.file_data = StrToByteArray(contents);
  ctx.chain_type = iControl.CommonFileChainType.FILE_FIRST_AND_LAST;
  m_interfaces.SystemConfigSync.upload_file(target_file, ctx);
}

```

Make sense?

-Joe

---

<div class="post-metadata">

**Author:** ![Nicolas\_Menant\_1](https://avatars.discourse-cdn.com/v4/letter/n/f19dbf/32.png) [@Nicolas\_Menant\_1](https://community.f5.com/u/Nicolas_Menant_1)\
**Post date:** [March 20, 2008, 7:59pm UTC](https://community.f5.com/t/structure-filetransfercontext-and-file-data/61991/3 "2008-03-20T19:59:00Z")

</div>

Hi,

It makes perfect sense! Thanks

i made a mistake by putting FILE\_FIRST\_AND\_LAST instead of FILE\_FIRST \>\<

and thanks for the function to translate a string in byte[].

I guess i’ll need to check all those translation type!

Thanks again

---

<div class="post-metadata">

**Author:** ![safiyet\_80777](https://avatars.discourse-cdn.com/v4/letter/s/b38774/32.png) [@safiyet\_80777](https://community.f5.com/u/safiyet_80777)\
**Post date:** [August 9, 2011, 8:18am UTC](https://community.f5.com/t/structure-filetransfercontext-and-file-data/61991/4 "2011-08-09T08:18:23Z")

</div>

Hi,

I want to this process with perl. How can I do?

Thanks.

---

<div class="post-metadata">

**Author:** ![Joe\_Pruitt](https://d1p9zq3aats0t8.cloudfront.net/user_avatar/community.f5.com/joe_pruitt/32/8438_2.png) [@Joe\_Pruitt](https://community.f5.com/u/Joe_Pruitt)\
**Post date:** [August 9, 2011, 3:50pm UTC](https://community.f5.com/t/structure-filetransfercontext-and-file-data/61991/5 "2011-08-09T15:50:16Z")

</div>

Take a look at the PerlConfigSync sample in the iControl CodeShare. That may point you in the right direction.

[http://devcentral.f5.com/wiki/iControl.PerlConfigSync.ashx](http://devcentral.f5.com/wiki/iControl.PerlConfigSync.ashx)

-Joe
