Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
wzj-java
uni-app
提交
81281856
U
uni-app
项目概览
wzj-java
/
uni-app
与 Fork 源项目一致
Fork自
DCloud / uni-app
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
81281856
编写于
12月 06, 2018
作者:
郭
郭胜强
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
fix: 解决本地选择的文件上传时丢失原始文件名的问题
上级
0e5e7208
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
59 addition
and
44 deletion
+59
-44
src/platforms/h5/service/api/file/util.js
src/platforms/h5/service/api/file/util.js
+36
-14
src/platforms/h5/service/api/media/choose-image.js
src/platforms/h5/service/api/media/choose-image.js
+4
-2
src/platforms/h5/service/api/media/choose-video.js
src/platforms/h5/service/api/media/choose-video.js
+4
-2
src/platforms/h5/service/api/network/download-file.js
src/platforms/h5/service/api/network/download-file.js
+2
-2
src/platforms/h5/service/api/network/upload-file.js
src/platforms/h5/service/api/network/upload-file.js
+13
-24
未找到文件。
src/platforms/h5/service/api/file/util.js
浏览文件 @
81281856
/**
* 从url读取Blob
* 暂存的文件对象
*/
const
files
=
{}
/**
* 从url读取File
* @param {string} url
* @param {Function} success
* @param {Function} error
* @param {Promise}
*/
export
function
urlToBlob
(
url
,
success
,
error
)
{
var
xhr
=
new
XMLHttpRequest
()
xhr
.
open
(
'
GET
'
,
url
,
true
)
xhr
.
responseType
=
'
blob
'
xhr
.
onload
=
function
()
{
success
(
this
.
response
)
export
function
urlToFile
(
url
)
{
var
file
=
files
[
url
]
if
(
file
)
{
return
Promise
.
resolve
(
file
)
}
xhr
.
onerror
=
error
xhr
.
send
()
if
(
/^data:
[
a-z-
]
+
\/[
a-z-
]
+;base64,/
.
test
(
url
))
{
return
Promise
.
resolve
(
base64ToBlob
(
url
))
}
return
new
Promise
((
resolve
,
reject
)
=>
{
var
xhr
=
new
XMLHttpRequest
()
xhr
.
open
(
'
GET
'
,
url
,
true
)
xhr
.
responseType
=
'
blob
'
xhr
.
onload
=
function
()
{
resolve
(
this
.
response
)
}
xhr
.
onerror
=
reject
xhr
.
send
()
})
}
/**
* base64转Blob
...
...
@@ -32,9 +44,19 @@ export function base64ToBlob (base64) {
}
/**
* 从本地file或者blob对象创建url
* @param {Blob|File}
blob
* @param {Blob|File}
file
* @return {string}
*/
export
function
blobToUrl
(
blob
)
{
return
(
window
.
URL
||
window
.
webkitURL
).
createObjectURL
(
blob
)
export
function
fileToUrl
(
file
)
{
for
(
const
key
in
files
)
{
if
(
files
.
hasOwnProperty
(
key
))
{
const
oldFile
=
files
[
key
]
if
(
oldFile
===
file
)
{
return
key
}
}
}
var
url
=
(
window
.
URL
||
window
.
webkitURL
).
createObjectURL
(
file
)
files
[
url
]
=
file
return
url
}
src/platforms/h5/service/api/media/choose-image.js
浏览文件 @
81281856
import
{
fileToUrl
}
from
'
../file/util
'
const
{
invokeCallbackHandler
:
invoke
}
=
UniServiceJSBridge
...
...
@@ -44,7 +46,7 @@ export function chooseImage ({
const
fileCount
=
event
.
target
.
files
.
length
for
(
let
i
=
0
;
i
<
fileCount
;
i
++
)
{
const
file
=
event
.
target
.
files
[
i
]
const
filePath
=
window
.
URL
.
createObjectURL
(
file
)
const
filePath
=
fileToUrl
(
file
)
tempFilePaths
.
push
(
filePath
)
tempFiles
.
push
({
...
...
@@ -63,4 +65,4 @@ export function chooseImage ({
})
imageInput
.
click
()
}
}
src/platforms/h5/service/api/media/choose-video.js
浏览文件 @
81281856
import
{
fileToUrl
}
from
'
../file/util
'
const
{
invokeCallbackHandler
:
invoke
}
=
UniServiceJSBridge
...
...
@@ -31,7 +33,7 @@ export function chooseVideo ({
videoInput
.
addEventListener
(
'
change
'
,
function
(
event
)
{
const
file
=
event
.
target
.
files
[
0
]
const
filePath
=
window
.
URL
.
createObjectURL
(
file
)
const
filePath
=
fileToUrl
(
file
)
invoke
(
callbackId
,
{
errMsg
:
'
chooseVideo:ok
'
,
...
...
@@ -43,4 +45,4 @@ export function chooseVideo ({
})
videoInput
.
click
()
}
}
src/platforms/h5/service/api/network/download-file.js
浏览文件 @
81281856
import
{
blob
ToUrl
}
from
'
../file/util
'
import
{
file
ToUrl
}
from
'
../file/util
'
/**
* 下载任务
*/
...
...
@@ -57,7 +57,7 @@ export function downloadFile ({
invoke
(
callbackId
,
{
errMsg
:
'
downloadFile:ok
'
,
statusCode
,
tempFilePath
:
blob
ToUrl
(
blob
)
tempFilePath
:
file
ToUrl
(
blob
)
})
}
xhr
.
onabort
=
function
()
{
...
...
src/platforms/h5/service/api/network/upload-file.js
浏览文件 @
81281856
import
{
urlToBlob
,
base64ToBlob
}
from
'
../file/util
'
import
{
urlToFile
}
from
'
../file/util
'
/**
* 上传任务
*/
...
...
@@ -52,32 +49,15 @@ export function uploadFile ({
invokeCallbackHandler
:
invoke
}
=
UniServiceJSBridge
var
uploadTask
=
new
UploadTask
(
null
,
callbackId
)
// 暂时只支持blob和base64
if
(
filePath
.
indexOf
(
'
blob:http
'
)
===
0
)
{
urlToBlob
(
filePath
,
upload
,
()
=>
{
setTimeout
(()
=>
{
invoke
(
callbackId
,
{
errMsg
:
'
uploadFile:fail file error
'
})
},
0
)
})
}
else
if
(
/^data:
[
a-z-
]
+
\/[
a-z-
]
+;base64,/
.
test
(
filePath
))
{
upload
(
base64ToBlob
(
filePath
))
}
else
{
setTimeout
(()
=>
{
invoke
(
callbackId
,
{
errMsg
:
'
uploadFile:fail filePath error
'
})
},
0
)
}
function
upload
(
blob
)
{
function
upload
(
file
)
{
var
xhr
=
new
XMLHttpRequest
()
var
form
=
new
FormData
()
var
timer
Object
.
keys
(
formData
).
forEach
(
key
=>
{
form
.
append
(
key
,
formData
[
key
])
})
form
.
append
(
name
,
blob
,
`file-
${
Date
.
now
()}
`
)
form
.
append
(
name
,
file
,
file
.
name
||
`file-
${
Date
.
now
()}
`
)
xhr
.
open
(
'
POST
'
,
url
)
Object
.
keys
(
header
).
forEach
(
key
=>
{
xhr
.
setRequestHeader
(
key
,
header
[
key
])
...
...
@@ -131,5 +111,14 @@ export function uploadFile ({
})
}
}
urlToFile
(
filePath
).
then
(
upload
).
catch
(()
=>
{
setTimeout
(()
=>
{
invoke
(
callbackId
,
{
errMsg
:
'
uploadFile:fail file error
'
})
},
0
)
})
return
uploadTask
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录