Valine icon indicating copy to clipboard operation
Valine copied to clipboard

如果我只想修复 图片上传的功能,不想改其他的,需要改哪些代码?

Open tzy13755126023 opened this issue 3 years ago • 3 comments

image

如果我只想修复 图片上传的功能,不想改其他的,需要改哪些代码? 并未找到 #396 #380 这部分的代码

tzy13755126023 avatar Jun 27 '22 09:06 tzy13755126023

你好,如果你是基于当前仓库的源码修改的,可以找到以下代码: https://github.com/xCss/Valine/blob/c40826c5816c98d797a6b1ed8b62bddf73ed4f65/src/index.js#L1045-L1068

将其修改为本地:

let uploadImage = (file,callback)=>{ 
    let reader = new FileReader();
    reader.onload = function(){
        // 这里能获取到文件的Base64
        console.log(reader.result)
        callback && callback(null,{data: reader.result})
    }
    reader.readAsDataURL(file)
 } 

如果你想优化显示,可以使用URL.createObjectURL来创建blob URL

let key = URL.createObjectURL(imageFile)

显示的时候可以用blob URL,最后提交的时候记得替换成Base64


参考:

xCss avatar Jun 29 '22 08:06 xCss

emmm 我的版本是 Valine v1.4.14

我找到了这部分代码 是否改这段代码  可以将 图片上传到我自己的图床呢 Q=function(e,t){   var n=new FormData;   n.append("image",e),   y.default.ajax({      type:"post",      url:"https://pic.alexhchu.com/api/upload",      data:n,      success:function(e){        t&&t(e)      } })}}

------------------ Original message ------------------ From: "YY"; Sendtime: Wednesday, Jun 29, 2022 4:15 PM To: "xCss/Valine"; Cc: @.***>; "Author"; Subject: Re: [xCss/Valine] 如果我只想修复 图片上传的功能,不想改其他的,需要改哪些代码? (Issue #403)

你好,如果你是基于当前仓库的源码修改的,可以找到以下代码: https://github.com/xCss/Valine/blob/c40826c5816c98d797a6b1ed8b62bddf73ed4f65/src/index.js#L1045-L1068

将其修改为本地: let uploadImage = (file,callback)=>{ let reader = new FileReader(); reader.onload = function(){ // 这里能获取到文件的Base64 console.log(reader.result) callback && callback(null,{data: reader.result}) } reader.readAsDataURL(file) }

如果你想优化显示,可以使用URL.createObjectURL来创建blob URL: let key = URL.createObjectURL(image)

参考:

URL.createObjectURL

FileReader

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

tzy13755126023 avatar Jun 30 '22 04:06 tzy13755126023

emmm 我的版本是 Valine v1.4.14 我找到了这部分代码 是否改这段代码  可以将 图片上传到我自己的图床呢 Q=function(e,t){   var n=new FormData;   n.append("image",e),   y.default.ajax({      type:"post",      url:"https://pic.alexhchu.com/api/upload",      data:n,      success:function(e){        t&&t(e)      } })}}

如果是要上传到你自己的图床,直接修改图床地址url:"https://pic.alexhchu.com/api/upload" 为你自己的地址就行了。

返回数据格式应该是类似于这样:

{
    'code': 200,
    'data': 'your image url',
    'msg': ''
}

你可以试试看~

xCss avatar Jun 30 '22 07:06 xCss