Base solution for your next web application
Open Closed

adding formdata to service call #8326


User avatar
0
BobIngham created

.net core, angular, 6.8.0 I am testing a proof of concept with Syncfusion's document editor and need to convert a .docx file to .sfdt file (syncfusion format) on the server side before editing. My service method looks as follows:

[DontWrapResult(WrapOnError = true)]
public string Import(IFormCollection data)
{
    if (data.Files.Count == 0)
        return null;
    Stream stream = new MemoryStream();
    IFormFile file = data.Files[0];
    int index = file.FileName.LastIndexOf('.');
    string type = index > -1 && index < file.FileName.Length - 1 ?
        file.FileName.Substring(index) : ".docx";
    file.CopyTo(stream);
    stream.Position = 0;

    WordDocument document = WordDocument.Load(stream, GetFormatType(type.ToLower()));
    string sfdt = Newtonsoft.Json.JsonConvert.SerializeObject(document);
    document.Dispose();
    return sfdt;
}

Following Syncfusion's instructions I have a call from angular to same which works fine as follows:

  public loadFile(file: File): void {
    let httpRequest: XMLHttpRequest = new XMLHttpRequest();
    httpRequest.open('POST', 'http://localhost:22742/api/services/app/NcSyncfusionDocumentEditor/Import', true);

    httpRequest.onreadystatechange = () => {
      if (httpRequest.readyState === 4) {
        if (httpRequest.status === 200 || httpRequest.status === 304) {
          this.documentContainer.documentEditor.open(httpRequest.responseText);
          this.documentContainer.documentEditor.isReadOnly = false;
        } else {
          alert('Fail to load the document');
          console.error(httpRequest.response);
        }
      }
    };
    let formData: FormData = new FormData();
    formData.append('files', file);
    httpRequest.send(formData);
  }

My question is how do I convert this call to use the swagger interface - how do I add the form data to the call?

    this._ncDocumentEditorService.import( ?? file ??)
      .subscribe(sfdtDocument => {
        this.documentContainer.documentEditor.open(sfdtDocument);
        this.documentContainer.documentEditor.isReadOnly = false;
      });

1 Answer(s)
  • User Avatar
    0
    BobIngham created

    Code was refactored, I am closing this issue to save anyone spending time on it.