• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2023 Hunan OpenValley Digital Industry Development 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 */
15import http from '@ohos.net.http';
16import Logger from '../utils/Logger';
17import Constant from '../utils/Constant';
18import NetworkModel from '../model/NetworkModel';
19import R from '../appsampled/data/R';
20
21const TAG: string = '[UploadController]';
22
23export default class UploadController {
24  private networkModel: NetworkModel = new NetworkModel();
25
26  public async uploadFile(fileName: string): Promise<void> {
27    Logger.info(TAG, `uploadFile start`);
28    let extraData = {
29      name: fileName,
30      url: `${Constant.UPLOAD_URL}/${fileName}`
31    };
32    Logger.info(TAG, `uploadFile extraData->${JSON.stringify(extraData)}`);
33    this.networkModel.uploadFile(Constant.ACTION_UPLOAD, fileName, (res) => {
34      if (res) {
35        Logger.info(TAG, `uploadFile success`);
36        // 上传成功后请求服务器添加文件
37        let data: ESObject= AppStorage.get("userInfo");
38        this.networkModel.request(Constant.ACTION_ADD_FILE, http.RequestMethod.POST, extraData, data?.token);
39      }
40    });
41  }
42}