Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/389.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用Ajax Web服务将文件路径从Javascript传递到C_C#_Javascript_Json - Fatal编程技术网

C# 使用Ajax Web服务将文件路径从Javascript传递到C

C# 使用Ajax Web服务将文件路径从Javascript传递到C,c#,javascript,json,C#,Javascript,Json,我有一个带有按钮的ASP.Net网站,这些按钮将文件路径传递给javascript函数,该函数执行对web方法的ajax调用。下面传递给openDoc的文件路径是JSON.stringify\fs01\Documents的结果 <input type="button" id="b0" value="Open" onclick="openDoc('&quot;\\\\fs01\\Documents\\2011\\MEASEJA\\1108030203&nbsp;(1)&a

我有一个带有按钮的ASP.Net网站,这些按钮将文件路径传递给javascript函数,该函数执行对web方法的ajax调用。下面传递给openDoc的文件路径是JSON.stringify\fs01\Documents的结果

  <input type="button" id="b0" value="Open" onclick="openDoc('&quot;\\\\fs01\\Documents\\2011\\MEASEJA\\1108030203&nbsp;(1)&nbsp;10-3-2011&nbsp;Mart&nbsp;Cart&nbsp;Service&nbsp;Completion&nbsp;Survey.pdf&quot;');">

    function openDoc(filename) {
    var xml = new XMLHttpRequest();
    xml.open("POST", "Default.aspx", true);
    xml.onreadystatechange = function () {
        if (xml.readyState == 4) {
            if (xml.status == 200) {
                $.ajax({
                    type: "POST",
                    dataType: "json",
                    contentType: "application/json",
                    url: "Service.asmx/openDoc",
                    data: '{ file:' + filename + '}',
                    success: function () { },
                    error: function (xml, status, error) {
                        var err = eval("(" + xml.responseText + ")");
                        alert("Open Doc: " + xml.Message);
                    }
                });
            }
        }
    }
    xml.send(null);
}

[WebMethod]
public byte[] openDoc(string file)
{
    FileStream fs = new FileStream(file, FileMode.Open, FileAccess.Read);
    byte[] buffer;

    try
    {
        int length = (int)fs.Length;
        buffer = new byte[length];
        int count;
        int sum = 0;

        while ((count = fs.Read(buffer, sum, length - sum)) > 0)
            sum += count;
    }
    catch (Exception ex)
    {
        throw (ex);
    }
    finally
    {
        fs.Close();   
    }

    return buffer;
}
问题是,在这个过程中的某个地方,文件路径中的\发生了一些事情,并从使用firebug的ajax调用中接收到如下错误:

无法识别的转义序列。16:{文件:\fs01\Documents\2011\MEASEJA\1108030203 1 10-3-2011购物车服务完成调查.pdf}


我知道在C语言中,您必须将所有的\加倍,但这就是文件路径从单击按钮开始的方式。在这个过程中的某个地方,我丢失了额外的,C无法将其作为文件处理

如果需要,尝试寻找一个反斜杠并加倍

//pseudocode
if (s.StartsWith(@"\f")) s.Prepend(@"\");

在我把它作为一个参数传递给ajax之前?实际上,先把它擦掉。现在我仔细看了看,我知道你想要什么了。我会尝试一下并让您知道。将if file.StartsWith@\f{file=@\+file;}添加到web方法的开头,但我得到了相同的错误。