简体   繁体   English

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

[英]Return a string from generic handler

I want to return a string from generic handler and get it into angularjs. 我想从通用处理程序返回一个字符串,并将其放入angularjs。 My code is below. 我的代码如下。 Generic handler: 通用处理程序:

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 Controller: 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.
}

How I can get file path. 我如何获取文件路径。

You want the XHR object's responseText . 您需要XHR对象的responseText

However, you can't get it where you have the comment, since XMLHttpRequest is typically asynchronous. 但是,由于XMLHttpRequest通常是异步的,因此无法在有注释的位置获得它。 You'd need something like 您需要类似

//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);

You should probably check the request status in there too. 您可能也应该在那里检查请求状态。 See the MDN XHR documentation for more info on that. 有关更多信息,请参见MDN XHR文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM