• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (C) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the 'License');
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an 'AS IS' BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16import {describe, it, expect} from 'deccjsunit/index'
17import request from '@ohos.request'
18
19let RequestData = [{
20    name: '', // Represents the name of the form element.
21    value: '' // Represents the value of the form element.
22}]
23
24let RequestDataArray=new Array();
25
26function sleep(ms) {
27    return new Promise(resolve => setTimeout(resolve, ms));
28}
29
30function getUploadConfig(fileurl){
31    let File = {
32        filename: 'test', // When multipart is submitted, the file name in the request header.
33        name: 'test', // When multipart is submitted, the name of the form item. The default is file.
34        uri: 'internal://cache/test.txt',
35        //The local storage path of the file
36        // (please refer to the storage directory definition for path usage).
37        type: 'txt'
38        //The content type of the file is obtained by default
39        // according to the suffix of the file name or path.
40    }
41    let FileArray=new Array();
42    FileArray[0] = File;
43    let headerHttp = { headers: 'http' }
44    let UploadConfig = {
45        url: 'http://192.168.112.124/upload_test/',// Resource address.
46        header: headerHttp, // Adds an HTTP or HTTPS header to be included with the upload request.
47        method: 'POST', // Request method: POST, PUT. The default POST.
48        files: FileArray, // A list of files to be uploaded. Please use multipart/form-data to submit.
49        data: RequestData // The requested form data.
50    }
51    return UploadConfig
52}
53
54//upload公共方法
55function publicupload(UploadConfig){
56    console.log(`Testupdate UploadConfig ${JSON.stringify(UploadConfig)}`)
57    return new Promise(function(resolve, reject) {
58        request.upload(UploadConfig, (err, data) => {
59            console.log("Testupdate publiconprogress Updatetask =" + JSON.stringify(data));
60            resolve(data);
61        })
62    })
63}
64
65//onprogress公共方法
66function publiconprogress(Updatetask, Type){
67    return new Promise(function(resolve, reject) {
68        Updatetask.on(Type, function(data1 ,data2){
69            let progress = {
70                uploadedSize : data1,
71                totalSize : data2
72            }
73            console.log("Testupdate publiconprogress uploadedSize =" + data1);
74            console.log("Testupdate publiconprogress totalSize =" + data2);
75            resolve(progress);
76        })
77    })
78}
79
80//offprogress公共方法
81function publicoffprogress(Updatetask, Type){
82    return new Promise(function(resolve, reject) {
83        Updatetask.off(Type, function(data1 ,data2){
84            let progress = {
85                uploadedSize : data1,
86                totalSize : data2
87            }
88            console.log("Testupdate publicoffprogress uploadedSize =" + data1);
89            console.log("Testupdate publicoffprogress totalSize =" + data2);
90            resolve(progress);
91        })
92    })
93}
94
95//其他on公共方法
96function publicon(Updatetask, Type){
97    return new Promise(function(resolve, reject) {
98        Updatetask.on(Type, function(data){
99            console.log("Testupdate publicon =" + data);
100            resolve(data);
101        })
102    })
103}
104
105//其他off公共方法
106function publicoff(Updatetask, Type){
107    return new Promise(function(resolve, reject) {
108        Updatetask.off(Type, function(data){
109            console.log("Testupdate publicoff =" + data);
110            resolve(data);
111        })
112    })
113}
114
115//remove公共方法
116function publicremove(Updatetask, Type){
117    return new Promise(function(resolve, reject) {
118        Updatetask.remove((err,data) => {
119            console.log("Testupdate publicremove =" + data);
120            resolve(data);
121        })
122    })
123}
124
125export{publicupload,publicon,publicoff,publicremove,publiconprogress,publicoffprogress,getUploadConfig,sleep}