Retrieve link of Binary file from Codebehind

Hi,

I make a call to my API from the code behind and it returns a set of files in bit stream format (byte []).

How can I get from these files clickable links that will allow the user to download files?Do I have to restore those files on disk?

Can I have these links in a column of a GridView?

Thank you for you support .

Best Regards,
Sara.

Hi @sara,

Send bit stream file.

You can use WebHooks to send a file in bit stream converted to a base64String.

Here is an example that I did to a client not too long ago:

public void RemoteProcessLaunch(object sender, EventArgs e)
        {
            string url = "http://site.com/wfgen/hooks/hookToken";

            var httpWebRequest = (HttpWebRequest)WebRequest.Create(url);
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";

            Stream fs = UPLOADED_FILE.PostedFile.InputStream;
            BinaryReader br = new BinaryReader(fs);
            Byte[] bytes = br.ReadBytes((Int32)fs.Length);
            string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                string json = "{" +
                                "\"operation\": \"createRequest\"," +
                                "\"args\": {" +
                                            "\"input\": {" +
                                                "\"processName\": \"PROCESS_NAME\"," +
                                                "\"processVersion\": 1," +
                                                "\"parameters\": ["{" +
                                                     "\"name\": \"FILE_ID1\"," +
                                                     "\"type\": \"FILE\"," +
                                                     "\"fileValue\":{" +
                                                     "\"name\": \"" + Path.GetFileName(UPLOADED_FILE.PostedFile.FileName) + "\"," +
                                                     "\"contentType\":\"" + UPLOADED_FILE.PostedFile.ContentType + "\"," +
                                                     "\"content\":\"" + base64String + "\"" +
                                            "}" +
                                        "}]" +
                                  "}" +
                                "}" +
                              "}";

                streamWriter.Write(json);
                streamWriter.Flush();
                streamWriter.Close();
            }

            try
            {
                var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
                using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
                {
                    var result = streamReader.ReadToEnd();
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
                throw;
            }
        }

You can also use the operation UpdateRequestDataset if you would like to update the process data instead of creating a new request.

Retrieve file URL and store it in GridView

You can then use GraphQL to retrieve the file URL and then display it in the GridView:

GraphQL query:

{
  request(number:23){
    dataset(filter:{names:["FILE_ID1","FILE_ID2"]}){
      items{
        name
        fileValue{
          url
        }
      }
    }
  }
}

You can add this in the Page_Load function in code behind:

//Create GraphQL query
string url = System.Configuration.ConfigurationManager.AppSettings["ApplicationUrl"] + 
"/graphql?query={ request(number:"+CURRENT_REQUEST.Text+"){ dataset(filter:{names:[\"FILE_ID1\",\"FILE_ID2\"]}){ items{ name fileValue{ url } } } } }";

//Send HTTP request to retrieve JSON result
System.Net.HttpWebRequest requestGraphQL = (System.Net.HttpWebRequest) System.Net.WebRequest.Create(url);
requestGraphQL.Credentials = new System.Net.NetworkCredential(APIUsername, APIPassword);
System.Net.HttpWebResponse response = (System.Net.HttpWebResponse) requestGraphQL.GetResponse();
System.IO.Stream receiveStream = response.GetResponseStream();
System.IO.StreamReader readStream = new System.IO.StreamReader(receiveStream, Encoding.UTF8);

//Convert JSON result to a dynamic JSON object
dynamic obj = Newtonsoft.Json.JsonConvert.DeserializeObject(readStream.ReadToEnd());

//Clear GridView each time to avoid duplicates
this.FormData.Tables["SectionID_GridViewID"].Rows.Clear();

//Autofill GridView with URL value of all files
foreach(var item in data.request.dataset.items){
    //Put validations to check if the url value is not null or empty
    System.Data.DataRow newRow = this.FormData.Tables["SectionID_GridViewID"].NewRow();
    newRow["SectionID_GridViewID_Field1"] = "1";
    newRow["SectionID_GridViewID_Field2"] = "2";

    //This is where we add the URL in the GridView
    newRow["SectionID_GridViewID_Field3"] = "<a href=\""  item.fileValue.url + "\" target=\"_blank\">View File<a>";
    this.FormData.Tables["SectionID_GridViewID"].Rows.Add(newRow);
}

// Accept Changes in the Form Data
this.FormData.Tables["SectionID_GridViewID"].AcceptChanges();
SaveFormData(this.FormData);
BindFormDataToFields(this.FormData);

Hope this helps.

Regards,
Eddy.