从通用处理程序返回一个字符串

时间:2014-01-08 05:30:25

标签: c# angularjs generics

我想从泛型处理程序返回一个字符串并将其转换为angularjs。我的代码如下。 通用处理程序:

public void ProcessRequest(HttpContext context)
{
    List<string>  path= new List<string>();
    if (context.Request.Files.Count > 0)
    {
        HttpFileCollection files = context.Request.Files;
        for (int i = 0; i < files.Count; i++)
        {
            HttpPostedFile file = files[i];
            string fname = context.Server.MapPath("~/upload/" + file.FileName);
            //file.SaveAs(fname);
            path.Add(fname);
        }
    }
    string abc = path[0].ToString();
    context.Response.ContentType = "text/plain";
    context.Response.Write(abc);
}

Angularjs控制器:

$scope.uploadFile = function () {
    var fd = new FormData()
    for (var i in ScopGloble.files) {
       // alert("hi I am in");
        fd.append("uploadedFile", $scope.files[i])
    }
    var xhr = new XMLHttpRequest()
    xhr.open("POST", "FileuploadTask.ashx")
    ScopGloble.progressVisible = true
    xhr.send(fd)
    //How I can get file path here.
}

我如何获取文件路径。

1 个答案:

答案 0 :(得分:1)

您想要XHR对象的responseText

但是,您无法获得评论的位置,因为XMLHttpRequest通常是异步的。你需要像

这样的东西
//before we send the request
xhr.onload = function(xhrEventArgs) {
  var ThePath = xhr.responseText;
  //do something with that string
};
//now, send the request
xhr.send(fd);

您也应该在那里检查请求状态。有关详细信息,请参阅MDN XHR documentation

相关问题