完成此任务的最佳方法是将 AsyncUpload 配置为使用自定义处理程序。然后在处理程序的 Process 方法中获得文件数据后,只需将其发送到 Web API 服务。
<telerik:RadAsyncUpload
ID="RadAsyncUpload1"
OnClientFileUploadFailed="OnClientFileUploadFailed"
OnClientValidationFailed="OnClientValidationFailed"
HttpHandlerUrl="~/Handler.ashx"
runat="server">
</telerik:RadAsyncUpload>
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using Telerik.Web.UI;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Data.SqlClient;
using System.IO;
public class Handler : AsyncUploadHandler, System.Web.SessionState.IRequiresSessionState
{
protected override IAsyncUploadResult Process(UploadedFile file, HttpContext context, IAsyncUploadConfiguration configuration, string tempFileName)
{
//here send the file to the WebAPI method
//and return custom result to the client
}
}
如果您想知道是否可以在客户端读取文件对象,那么您应该知道这仅适用于支持 HTML5 File API 的较新浏览器。