使用Http post将FormData发送到WebMethod [英] Send FormData To WebMethod using Http post

查看:317
本文介绍了使用Http post将FormData发送到WebMethod的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在尝试使用HttpPost将xml文件发送到Web方法但是在wbmethod中我得到例外,

  var  file = HttpContext.Current.Request.Files [ 0 ] 



因为文件数量为零,任何人都可以帮忙吗?

 $ scope.SelectFileForUpload =  function (文件) {
if (file!= undefined && file!= null && file.length!= 0 ){
if (file.length< 2 ){
if (file [ 0 ]。type == ' text / xml' ){
$ scope.TransitionOfCareImport.File = file [ 0 ];
$ scope.GenerateHtml($ scope.TransitionOfCareImport.File);
}
else {
alert( 仅允许Xml);

angular.forEach(angular.element( input [type ='file'] ), function (inputElem){
angular.element(inputElem).val( null );
});
}
}
其他 {
alert( 不允许多个文件);
}
}


var formDaata = new FormData();
formDaata.append( file,file);


http({
url:myUrl,
method:method,
contentType: application / json
data:formDaata,
dataType: json

})
.success( function (数据,状态,标题,配置){
alert( success);

})
.error( function (数据,状态,标题,配置){
alert(data.Message) ;
});
}



和我的webmethod:



 WebMethod (EnableSession =  true )] 
[ScriptMethod(ResponseFormat = ResponseFormat.Json,UseHttpGet = false )]
public static string GenerateHtmlFromXml()
{
if (HttpContext.Current.Request.Files!= null
{
var file = HttpContext.Current.Request.Files [ 0 < /跨度>];
}

return string .Empty;
}

解决方案

scope.SelectFileForUpload = function (文件){
if (file!= undefined &&& file!= null && file.length!= 0 ){
if (file.length< 2 ){
if ( file [ 0 ] .type == ' text / xml '){


scope.TransitionOfCareImport.File = file [ 0 ];


scope.GenerateHtml(

Hello i am trying to send xml file to the web-method using HttpPost but in the wbmethod i am getting exception in ,

var file = HttpContext.Current.Request.Files[0] 


because Files count is zero , can any one help ?

$scope.SelectFileForUpload = function (file) {
        if (file != undefined && file != null && file.length != 0) {
            if (file.length < 2) {
                if (file[0].type == 'text/xml') {                    
                    $scope.TransitionOfCareImport.File = file[0];
                    $scope.GenerateHtml($scope.TransitionOfCareImport.File);
                }
                else {
                    alert("Xml Allowed Only");

angular.forEach(angular.element("input[type='file']"),function (inputElem) {
   angular.element(inputElem).val(null);
});
}
            }
            else {
                alert("Multiple files are not allowed");
            }
        }


var formDaata = new FormData();
formDaata.append("file", file);


http({
        url: myUrl,
        method: method,
        contentType: "application/json",
        data: formDaata ,
        dataType: "json",

    })
        .success(function (data, status, headers, config) {
            alert("success");
            
        })
        .error(function (data, status, headers, config) {
            alert(data.Message);            
        });
    }


and in my webmethod:

WebMethod(EnableSession = true)]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = false)]
        public static string GenerateHtmlFromXml()
        {
            if (HttpContext.Current.Request.Files != null)
            {
                 var file = HttpContext.Current.Request.Files[0];
            }

            return string.Empty;
        }

解决方案

scope.SelectFileForUpload = function (file) { if (file != undefined && file != null && file.length != 0) { if (file.length < 2) { if (file[0].type == 'text/xml') {


scope.TransitionOfCareImport.File = file[0];


scope.GenerateHtml(


这篇关于使用Http post将FormData发送到WebMethod的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆