1

I have an Infopath Form Template on Sharepoint, I want to add a button there so when the user clicks on it, it will POST an string to the following Web API. The following web API is tested and returns an excel file as shown:

I want to Post the FileName of the excel file using post request and it is important for me the request method to be POST type. and then the user will download a file with the specified 'FileName'. Actually i want to use post method because at the next stage i will send the content of the excel file too.

Important Note: I only can use .Net FrameWork 3.5 because this is the only framework supported in InfoPath Form Templates.

[HttpPost]
public HttpResponseMessage Post([FromBody]string FileName)  
    {  
        string reqBook = "c:\somefile.xlsx";  
        //converting Excel(xlsx) file into bytes array  
        var dataBytes = File.ReadAllBytes(reqBook);  
        //adding bytes to memory stream   
        var dataStream = new MemoryStream(dataBytes);  

        HttpResponseMessage httpResponseMessage = Request.CreateResponse(HttpStatusCode.OK);  
        httpResponseMessage.Content = new StreamContent(dataStream);  
        httpResponseMessage.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");  
        httpResponseMessage.Content.Headers.ContentDisposition.FileName = FileName;  
        httpResponseMessage.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");  

        return httpResponseMessage;  
    }  
3
  • If you just want to call an API from c#, learn.microsoft.com/en-us/aspnet/web-api/overview/advanced/… Commented Feb 27, 2018 at 14:32
  • Possible duplicate of Download an Excel file Commented Feb 27, 2018 at 14:32
  • no it is not duplicate, because they only download excel file but it want to send the file name using post request. and then download that file. i dont want to use get method because in the next stages i also want to specify the content of the excel file in the post request Commented Feb 27, 2018 at 14:34

1 Answer 1

2

When you perform the HttpPost on the client side, you will want to read the HttpResponseStream to get the byte data of the response stream.

Once you have the response stream data, you can then deserialize it to the type of object in C# you want, or you could alternatively just write it to the disk as

File.WriteAllBytes("someexcel.xlsx",data);

An easy way to do it would be with the HttpClient class.

HttpClient client = new HttpClient();
var response = client.PostAsync("", null).Result;
var content = response.Content.ReadAsByteArrayAsync().Result;
File.WriteAllBytes("excel.xlsx", content);

Just fill in the PostAsync bit with the Url and the content you wish to post.

I am using .Result to keep everything synchronous - but you can use 'await' if you prefer.

If you are working with HttpWebRequests - then the process becomes more complicated, as you need to manage the streams yourself. The HttpClient will manage and handle it all for you - so I recommend it, unless there is something special it needs to do that it currently does not.

Due to your .Net 3.5 requirement:

private static HttpWebResponse MakeRequest(string url, string postArgument)
    {

        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);

        request.Method = "POST";

        request.ContentType = "multipart/form-data;";
        Stream stream = request.GetRequestStream();
        string result = string.Format("arg1={0}", postArgument);
        byte[] value = Encoding.UTF8.GetBytes(result);
        stream.Write(value, 0, value.Length);
        stream.Close();

        return (HttpWebResponse)request.GetResponse();
    }

You can then do:

var response = MakeRequest("http://mywebsite.com/ProcessExcel", "accounts.xlsx");

And then do

Stream objStream = response .GetResponseStream(); 
BinaryReader breader = new BinaryReader(objStream); 
byte[] data= breader.ReadBytes((int)webresponse.ContentLength); 
File.WriteAllBytes("excel.xlsx",data);
Sign up to request clarification or add additional context in comments.

5 Comments

.Net Framework 3.5 witch is usable in infopath form templates does not support PostAsync Method.
If PostAsync is not available, then the Post method will suffice.
Actually The WebClient does not have neither PostAsync not Post method.
Updated to accomodate the .net 3.5 requirement
I Implemented this function but at the last line of the MakeRequest method it throws this Exception: "the remote server returned and error: (415) Unsupported Media Type"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.