使用 AJAX Post 将 Excel 文件传递​​到服务器

我只是希望能够使用 html 输入类型=“文件”来选择 Excel 文件


<input type="file" id="UploadedFile" name="UploadedFile" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet">

然后将所选文件传递回服务器 - 最好使用 AJAX post:


    var serviceURL = appRoot + 'Register/ImportTasks'


    $j.ajax({

        type: "post",

        url: serviceURL,

        data: (??? Not sure how to present here ???),

        contentType: "application/json; charset=utf-8",

        dataType: "json",

        success: successFunc,

        error: errorFunc

    });

具体来说,我看不到如何将“文件”作为数据呈现给 AJAX 调用。


    public void ImportTasks(DataType?? uploadedExcelFile)

    {

        ..... Doing stuff ...

    }

然后我不确定我应该告诉方法在调用它时期望什么参数数据类型?


当年话下
浏览 85回答 1
1回答

宝慕林4294392

这是一个基本示例。你应该使用FormData&nbsp; &nbsp; &nbsp;var formData = new FormData();&nbsp; &nbsp; &nbsp;var uploadFiles = document.getElementById('js-upload-files').files;&nbsp; &nbsp; &nbsp;this.formData.append("MyKey", uploadFiles[0]);&nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; type: "POST",&nbsp; &nbsp; &nbsp; &nbsp; url: 'Controller/Upload',&nbsp; &nbsp; &nbsp; &nbsp; data: formData,&nbsp; &nbsp; &nbsp; &nbsp; dataType: 'json',&nbsp; &nbsp; &nbsp; &nbsp; contentType: false,&nbsp; &nbsp; &nbsp; &nbsp; processData: false,&nbsp; &nbsp; &nbsp; &nbsp; complete: this.onComplete.bind(this)&nbsp; &nbsp; });编辑忘记控制器代码&nbsp; &nbsp; [HttpPost]&nbsp; &nbsp; public virtual BetterJsonResult Upload()&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; foreach (var fileKey in Request.Files)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ...Request.Files[fileKey.ToString()] //access it like this&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Html5