最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Issue with CSV Download Functionality When Filename Contains a Dot - Stack Overflow

programmeradmin1浏览0评论

I'm implementing a feature in my web application to download CSV files, but I've encountered an issue where the file extension displays as ".bb" instead of ".csv" when the filename includes a dot. Here is the code snippet I'm using:

const csvData = new Blob(  
  [[].map(row => row.join(",")).join("\n")],  
  { type: "text/csv;charset=utf-8;" }  
);  
const link = document.createElement("a");  
link.href = window.URL.createObjectURL(csvData);  
link.download = "aa.bb";  
link.click(); 

I know I can change link.download = "aa.bb.csv" to include the correct file extension, but are there any other solutions to fix this issue?

I'm implementing a feature in my web application to download CSV files, but I've encountered an issue where the file extension displays as ".bb" instead of ".csv" when the filename includes a dot. Here is the code snippet I'm using:

const csvData = new Blob(  
  [[].map(row => row.join(",")).join("\n")],  
  { type: "text/csv;charset=utf-8;" }  
);  
const link = document.createElement("a");  
link.href = window.URL.createObjectURL(csvData);  
link.download = "aa.bb";  
link.click(); 

I know I can change link.download = "aa.bb.csv" to include the correct file extension, but are there any other solutions to fix this issue?

Share Improve this question asked Jan 20 at 7:55 foxirisfoxiris 3,37837 silver badges35 bronze badges 0
Add a comment  | 

1 Answer 1

Reset to default 2

You're literally asking the browser to download the file as aa.bb – it's doing exactly what you're asking it to.

A "file extension" is just a convention ("the last dot-separated segment of the file's name, if any", more or less) – just like you can't convert files from JPEG to, say, WAV by renaming them from .jpg to .wav, the file's contents ("CSV-ness") doesn't change.

In other words, if you want the browser to save a file that's aa.bb.csv, make that download attribute "aa.bb.csv". Why should there be another solution?

发布评论

评论列表(0)

  1. 暂无评论