0

Okay, this is different than other posts I'm seeing. I'm not trying to first open an Excel file and parse the contents into a Json object. I'm trying to take the file and convert it to a stream object of some sort or byte[] and then convert that to Json so I can use it as an input parameter to a POST method for a WebAPI.

Here is the full scenario.

I have clients that will use an internal-only website to select one or more Excel files. The workstations the users work on may or may not have Excel installed, thus, all of my Excel processing has to be done on the server. Once the Excel files are processed, they are combined into a System.Data.DataTable and the values are aggregated into one master report. This aggregated report needs to be returned to the client system so it can be saved.

I currently have this site working just fine in ASP.NET using C#. However, I need the "guts" of the website to be a WebAPI so that automation programs I have can make calls directly to the WebAPI and accomplish the same task that the internal-only website does. This will allow all processing for this sort of task to run through one code base (right now, there are about 4 versions of this task and they all behave differently, providing differing output).

The way I thought to do this was to, from the client, convert the Excel files to an array of System.IO.MemoryStream objects, then serialize the full array as a Json.NET stream and upload the stream to the webserver where it will be deserialized back into an array of MemoryStream. Once that is done, I can iterate the array and process each Excel file by the MemoryStream.

My problem is I can't figure out how to convert the MemoryStream[] into Json and then deserialize that up on the server.

breusshe
  • 59
  • 1
  • 15
  • Are you talking about csv or actual excel files (xls(x)/openxml)? What have you tried and where are you stuck ? May I suggest reading this: https://stackoverflow.com/help/how-to-ask – jeroenh Aug 13 '18 at 18:45
  • 1
    Json.NET will serialize a `byte []` byte array as a Base64 string, as explained in the [serialization guide](https://www.newtonsoft.com/json/help/html/serializationguide.htm#PrimitiveTypes). Is that what you need? – dbc Aug 13 '18 at 18:46
  • I think you might have hit the nail on the head, @dbc – breusshe Aug 13 '18 at 19:32
  • Other options include [this answer to *FileUpload to FileStream*](https://stackoverflow.com/a/3082505/344280) and [this answer to *How To Accept a File POST*](https://stackoverflow.com/a/19746825/344280). – dbc Aug 13 '18 at 21:57

1 Answers1

0

Rather than trying to pass the excel file around as JSON let the user upload the file to the server and then process it from there.

In the JSON rather than giving the content of the file put a link to the file.

  • Yeah, I thought about that, but I don't want to generate files on the webserver, even if I'll be deleting them later. Our servers don't get a lot of TLC so performing a significant amount of read-write operations to store the file local on the server temporarily would add up over time to an unstable server. – breusshe Aug 13 '18 at 19:32
  • We use a samba network share to help with some of those issues. Having the actual file on disk makes debugging down the road significantly easier too as you can see and test the file that was uploaded rather than having to track down an example from the user. – DanOnTheTrail Aug 13 '18 at 19:59