Javascript 通过导出uri而不依赖浏览器下载

Javascript 通过导出uri而不依赖浏览器下载,javascript,c#,excel,web-services,export-to-excel,Javascript,C#,Excel,Web Services,Export To Excel,我使用常用的方法将html表格导出到excel表格中,如下所示: var tableToExcel = function () { var uri = 'data:application/vnd.ms-excel;base64,' , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xml

我使用常用的方法将html表格导出到excel表格中,如下所示:

var tableToExcel = function () {
     var uri = 'data:application/vnd.ms-excel;base64,'
    , template = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body><table>{table}</table></br></body></html>'
    , base64 = function (s) { return window.btoa(unescape(encodeURIComponent(s))) }
    , format = function (s, c) { return s.replace(/{(\w+)}/g, function (m, p) { return c[p]; }) }

     return function (data, name) {
         var ctx = { worksheet: name || 'Worksheet', table: data }
         var url = uri + base64(format(template, ctx));
         return url;
     }
}()
在服务器上:

public string SaveFile(string url)
{
    try 
    {
        using (WebClient client = new WebClient())
        {
            client.DownloadFile(url, filePath);
        }
    }
    catch (Exception e)
    {
        string s = e.ToString();
    }
    return "";
}
但它抛出了一个异常,uri格式不正确。当浏览器点击uri时,它工作正常。但我想通过代码保存它,而不依赖于浏览器

1)通过网络客户端连接到服务器 2) WebClient在C驱动器上保存文件(它将创建或重写)

例如:

WebClient download = new WebClient();
download.DownloadFile("https://i-msdn.sec.s-msft.com/dynimg/IC822539.jpg", "C:/Users/<UserName>/img.jpg");
WebClient下载=新建WebClient();
下载。下载文件(“https://i-msdn.sec.s-msft.com/dynimg/IC822539.jpg“,“C:/Users//img.jpg”);
WebClient需要协议前缀:http://https://ftp://

WebClient download = new WebClient();
download.DownloadFile("https://i-msdn.sec.s-msft.com/dynimg/IC822539.jpg", "C:/Users/<UserName>/img.jpg");