我的用戶可以上傳影像檔案,可以是 SVG。
我需要確保所有 SVG 都具有寬度和高度屬性,以應對稍后發生的事情。
如果我將 SVG 作為檔案(通過輸入型別 =“檔案”),如果它丟失,我該如何添加它的寬度和高度?
uj5u.com熱心網友回復:
我最終做了以下事情,盡管我確信它可以以更好的方式改進/完成
let svgReader = new FileReader();
svgReader.onload = function (e) {
let svg = $(e.target.result);
for(let i =0;i<svg.length;i ) {
if (svg[i] instanceof SVGElement) {
svg = $(svg[i]);
break;
}
}
let width = svg.attr("width");
let height = svg.attr("height");
if (height == null || height == undefined) {
svg.attr("height", 300);
}
if (width == null || width == undefined) {
svg.attr("height", 300);
}
let svgFile = new XMLSerializer().serializeToString(svg[0]);
let new file = new File([svgFile], files[index].name, {type: "image/svg xml"});
};
svgReader.readAsText(files[0]);
我必須通過 SVG 找到 SVG 元素的實體,因為我測驗的一些 SVG 有額外的標簽或注釋,否則會搞砸。此外,寬度和高度的 300 值剛剛組成,但似乎沒問題,可能可以使用視圖框尺寸代替,但它適用于我
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/419549.html
標籤:
