Skip to content

Commit 10939df

Browse files
committedDec 23, 2016
feat: upad client upload example
use ajax upload
1 parent 4c8dfc5 commit 10939df

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed
 

‎.editorconfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
root = true
22

3-
[*.php]
3+
[*.{php,html}]
44
indent_style = space
55
indent_size = 4
66
end_of_line = lf

‎examples/client-upload/index.html

+25-7
Original file line numberDiff line numberDiff line change
@@ -27,19 +27,37 @@
2727
<fieldset>
2828
<legend>Client Upload Demo</legend>
2929
<input name="file" type="file">
30-
<input name="policy" type="hidden" id="policy">
31-
<input name="signature" type="hidden" id="signature">
32-
<input type="submit" value="submit" class="submit" id="submit">
30+
<input type="button" value="submit" class="submit" id="upload">
3331
</fieldset>
3432
</form>
3533

3634
<script>
3735
// 文件保存的路径
3836
var save_path = '/test/filename.txt';
39-
// 获取 policy 和 secret
40-
$.getJSON('http://localhost:9000/policy.php?save_path=/test/filename.txt', function (data) {
41-
document.getElementById('policy').value = data.policy;
42-
document.getElementById('signature').value = data.signature;
37+
$('#upload').on('click', function() {
38+
// 获取 policy 和 secret
39+
$.getJSON('http://localhost:9000/policy.php?save_path=' + save_path, function (data) {
40+
var uploadData = new FormData($('#demoForm')[0]);
41+
uploadData.append('policy', data.policy);
42+
uploadData.append('signature', data.signature);
43+
$.ajax({
44+
url: '//v0.api.upyun.com/sdkimg',
45+
type: 'POST',
46+
data: uploadData,
47+
cache: false,
48+
processData: false,
49+
contentType: false,
50+
}).done(function(data, textStatus) {
51+
alert('upload success');
52+
}).fail(function(res, textStatus, error) {
53+
try {
54+
var body = JSON.parse(res.responseText);
55+
alert('error: ' + body.message);
56+
} catch(e) {
57+
console.error(e);
58+
}
59+
});
60+
});
4361
});
4462
</script>
4563
</body>

0 commit comments

Comments
 (0)