0

我正在尝试使用 jQuery Ajax 将单个文件(不是图像)上传到 WCF Web 服务(svc)。我终于得到了 web.config 的传输权,我的 SVC 正在接收一些东西,但我不知道如何获取和处理传入的表单内容......

<input id="ipt_file" type="file" />
<a href='#' onclick="UploadFile();" data-role='button'>Upload</a>

var UploadFile = function () {
    var file_object = $('#ipt_file')[0].files[0];
    var form_data = new FormData();
    form_data.append('file', file_object);

    var xhr_upload = $.ajax({
        type: "POST",
        headers: { "Cache-Control":"no-cache", "X-Session":"12345678" },
        url: "../Upload.svc/UploadJobFile",
        data: form_data,
        processData: false,
        contentType: false,
        dataType: "json",
        success: function (msg) {
            if (typeof (msg) === "object") {
                var _upload = $.parseJSON(msg.d);
                alert(_upload.status + ': ' + _upload.msg);
            };
        }
    });
};

[ServiceContract]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Upload
{
    [OperationContract(Name="UploadJobFile")]
    [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedResponse, ResponseFormat = WebMessageFormat.Json)]
    [return: MessageParameter(Name = "d")]
    public string UploadJobFile() 
    {
        string response = string.Empty;
        HttpRequest request = HttpContext.Current.Request;
        string session_id = request.Headers["X-Session"];
        //
        // ??? need the request file or body
        //
        if (request.ContentLength > 0)
        {
            response = "OK, content length= " + request.ContentLength.ToString(); // works, returns ~ file size
        }
        else
        {
            response = "fail";
        }
        return response;
    }
}

不幸的是,request.Form["file"]、request.Form[0] 和 request.Params 似乎不起作用,尽管我已经阅读了许多其他声明。如果我无法访问请求正文,则无法创建字节数组、流或将其声明为 HttpPostedFile...(实际上我要做的只是设置一个新请求以将其传递给数据服务)。

我错过了什么?

4

0 回答 0