'Download a file through API controller of Word365 addin

flow diagram

I'm developing a Word 365 addin. The diagram above shows one of a functional flow.

Step 1 - User will click on a ribbon bar button in addin and javascript will call .NET web API controller with relevant details(file id)

Step 2 - controller will send a GET request to outside REST API to fetch file data

Step 3 - controller will have file data response (byte array)

Strp 4 - need to create a file and download it to the client machine. If possible, need to open file browser in client machine to choose a folder to download

So, I'm having problems with downloading the file to the client machine. How to download the file created inside API controller to client machine?



Solution 1:[1]

I tried these codes in winform as your addin client:

private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                string filePath = ofd.FileName;
                var filecontent = new ByteArrayContent(File.ReadAllBytes(filePath));
                var httpclient = new HttpClient();
                var requestContent = new MultipartFormDataContent();
                requestContent.Add(filecontent, "file","x.txt");
                var response = httpclient.PostAsync("https://localhost:44353/WeatherForecast", requestContent).Result;
               

                //Get the filecontent
                var responsebody = response.Content.ReadAsStringAsync().Result;
                byte[] array = Encoding.UTF8.GetBytes(responsebody);
                //Savefiledialog
                SaveFileDialog sfd = new SaveFileDialog();
                //Set file types
                sfd.Filter = "DOC?*.doc?|*.doc|Txt?*.txt?|*.txt";
                sfd.FilterIndex = 1;
                //Set the last path
                sfd.RestoreDirectory = true;
                //Set default filename
                sfd.FileName = "Hellow2";
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    //Get the filepath you selected
                    string localFilePath = sfd.FileName.ToString();
                    //Get the filename withoutpath
                    string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);
                    //Save file
                    FileStream fs;
                    if (System.IO.File.Exists(localFilePath))
                    {
                        fs = new FileStream(localFilePath, FileMode.Truncate);
                    }
                    else
                    {
                        fs = new FileStream(localFilePath, FileMode.CreateNew);
                    }
                    var br = new BinaryWriter(fs);
                    br.Write(array, 0, array.Length);
                    br.Close();
                    fs.Close();
                }
            }
        }

The codes in webapi:

[HttpPost]
        public FileResult txt([FromForm]IFormFile file)
        {
            var stream=file.OpenReadStream();
            byte[] bytes = new byte[stream.Length];
            stream.Read(bytes, 0, bytes.Length);
            stream.Seek(0, SeekOrigin.Begin);
            byte[] filecontent = bytes;
            return File(filecontent, "text/plain");
        }

The codes need more logical codes to be accomplished . also,with different file types, some other packages are required(for example, npoi for office). the length of filecontent was not considered as well.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1