Can';t通过javascript单击下载csv文件

Can';t通过javascript单击下载csv文件,javascript,Javascript,我试图通过单击按钮下载文件,但返回的文件名为“download”,没有任何扩展名,而不是“myFile.csv”。我也尝试了以下解决方案 这是服务器响应 var result = engine.WriteString(recs); response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(Encoding.UTF8.GetBytes(

我试图通过单击按钮下载文件,但返回的文件名为“download”,没有任何扩展名,而不是“myFile.csv”。我也尝试了以下解决方案

这是服务器响应

            var result = engine.WriteString(recs);
            response = new HttpResponseMessage(HttpStatusCode.OK) { Content = new ByteArrayContent(Encoding.UTF8.GetBytes(result)) };
            response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
            response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
            {
                FileName = String.Format("TVF_Export_{0}.csv", DateTime.Now.ToShortDateString())
            };
            return response;
客户端代码

        $http.post(url, filters).success(function (data, status, headers, config) {
        console.log(data);
        var hiddenElement = document.createElement('a');

        hiddenElement.href = 'data:attachment/csv,' + encodeURI(data);
        hiddenElement.target = '_blank';
        hiddenElement.download = 'myFile.csv';
        hiddenElement.click();


    }).error(function (data, status, headers, config) {
        console.log('[DEBUG] error ' + data);
    });

如果您可以控制服务器响应,则可以确保在响应中设置以下标头:

Content-Disposition: attachment; filename=myFile.csv;
您还应指定
内容类型:text/csv
或以下内容:

Content-Type: application/force-download

更多的信息将是有用的。您从哪里下载文件,从服务器端设置了哪些http头?数据对象只是一个字符串内容,这并不重要。您必须告诉我们,服务器如何响应您的下载请求。从服务器发送给您的HTTP头是什么?通常是HTTP头控制这样的内容。对于POST头动词,您的客户端代码是什么样子的?