Upload Excel file to Web API

I am trying to upload an excel file to my WebApi and DO STUFF with its contents. I have an extJs part submitting a file using a upload button. (code below)

My problem: I don't know how to create a webApi part to handle the excel file. I am assuming I must have an HttpPost.

Fake WebApi:

public string SampleUploadFile()
{
    return _repo.UploadFile();
}

      

Extjs code:

xtype: 'form',
//renderTo: 'fi-form', //(5)
fileUpload: true, //(1)
width: 500,
frame: true,
title: 'Position Sheet Upload Form',
bodyPadding: '10 10 0',
//bodyStyle: 'padding: 10px 10px 0 10px;',

defaults: {
    anchor: '100%',
    allowBlank: false,
    msgTarget: 'side',
    labelWidth: 50
},

//labelWidth: 50,
items: [{
    xtype: 'fileuploadfield',
    emptyText: 'Select an image',
    fieldLabel: 'Image',
    name: 'file', //(2)
    buttonText: 'Choose a file'
}],
buttons: [{
    text: 'Save',
    handler: function () {
        if (this.up('form').getForm().isValid()) {
            this.up('form').getForm().submit({
                url: 'Home/Upload',
                waitMsg: 'Uploading your file...',
                success: function (form, o) //(3)
                {
                    Ext.Msg.show({
                        title: 'Result',
                        msg: o.result.result,
                        buttons: Ext.Msg.OK,
                        icon: Ext.Msg.INFO
                    });
                },
                failure: function (form, o) //(4)
                {
                    Ext.Msg.show({
                        title: 'Result',
                        msg: o.result.error,
                        buttons: Ext.Msg.OK,
                        icon: Ext.Msg.ERROR
                    });
                }
            });
        }
    }
}]

      

Does anyone know how to do this? Should I have a parameter that is a file?

+3


source to share


2 answers


You can process files like this:



public void UploadFile(HttpRequestMessage request)
{
    HttpContext context = HttpContext.Current;
    HttpPostedFile postedFile = context.Request.Files["file"];
    // ...
}

      

+3


source


Another possible solution. The web API method uses the ClosedXML nuget package to read a stream containing excel content:



using ClosedXML.Excel;

public async void UploadFile()
{
    var context = HttpContext.Current;

    if (!Request.Content.IsMimeMultipartContent())
    {
        throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
    }

    var provider = new MultipartMemoryStreamProvider();
    await Request.Content.ReadAsMultipartAsync(provider);

    foreach (HttpContent ctnt in provider.Contents)
    {
        //now read individual part into STREAM
        var stream = await ctnt.ReadAsStreamAsync();
        if (stream.Length != 0)
        {
            //handle the stream here
            using (XLWorkbook excelWorkbook = new XLWorkbook(stream))
            {
                var name = excelWorkbook.Worksheet(1).Name;
            }
        }
    }
}

      

0


source







All Articles