0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Content-Dispositionのデコードに見るリーダブルコードとは

Posted at

Content-Dispositionのデコード例

なにやってるのかわからんコード

let headerLine = axiosResponse.headers['Content-Disposition'] || axiosResponse.headers['content-disposition'];
const fnameArr = headerLine.split(';');
let filename = fnameArr[fnameArr.length - 1].split('=')[1].replace('"', '').replace('"', '');
if (filename.indexOf(utf8Name) !== -1) {
    filename = decodeURI(filename.replace(utf8Name, ''));
  }
return filename

リーダブルなコード

var filenameRegex = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/;
var matches = filenameRegex.exec(res.headers["content-disposition"]);
    if (matches != null && matches[1]) {
      fileName = matches[1].replace(/['"]/g, '');
    }
return filename
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?