将Excel文件上传到Web API

sol*_*ssf 3 excel extjs asp.net-web-api

我正在尝试将excel文件及其内容上传到我的WebApi和DO STUFF。我已经完成了通过上传按钮发送文件的extJs部分。(下面的代码)

我的问题是我不知道如何构建webApi部分来处理excel文件。我猜我必须有一个HttpPost。

假WebApi:

public string SampleUploadFile()
{
    return _repo.UploadFile();
}
Run Code Online (Sandbox Code Playgroud)

Extjs代码:

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
                    });
                }
            });
        }
    }
}]
Run Code Online (Sandbox Code Playgroud)

有谁知道如何做到这一点?我应该有一个文件参数吗?

Lud*_*ltz 5

您可以处理以下文件:

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