【问题标题】:Image upload using typescript in mobile form data return none使用移动表单数据中的打字稿上传图像返回无
【发布时间】:2017-10-04 13:03:48
【问题描述】:

我是 typescript 的新手,正在尝试从我的移动 SD 卡上传图像。浏览图像时我正在获取图像路径。当我将它与表单数据一起传递时,None 正在返回。如果有人伸出援助之手来解决这个问题,那就太好了。

我正在使用“nativescript-imagepicker”库从 SD 卡获取图像..

sendPicture(uri: string, modelContext: any){
  let _formData = new FormData();
  _formData.append("profile_image", uri);
  let body = _formData;
  updateAvatarService(this.userToken,body).subscribe(data => {
  });
}

updateAvatarService(token,body): Observable<any> { 
            return this.httpClass.patchMethodWithToken(URL,token,body)
            .map(response => {
             return response;
           }) 
    }

patchMethodWithToken(url: string, token: string,  data: Object ) {
    let headers = new Headers();
   headers.append('Content-Type', 'application/json');
    headers.append('Authorization', "Token " + token);
    headers.append('Content-Disposition', "form-data");    
        let options = new RequestOptions({ headers: headers });
    if (this.checkNetworkConnection()) {
      return this.http
        .patch(url, JSON.stringify(data), options)
        .map(response => {
          return response.json();
        })
        .catch(this.handleErrors);
    }
  }

【问题讨论】:

  • 使用代码提供更多信息
  • 请检查我现在添加的代码..

标签: android ios typescript mobile nativescript


【解决方案1】:

对于作为表单数据的分段上传,我使用的是 Nativescript-background-Http 下面是示例实现,它对我有用 -

import { session, Session, Task } from "nativescript-background-http";
var session1 = session("image-upload");

uploadImage(fileUri, id) {

let imageName = this.extractImageName(fileUri);
let headers = new Headers();
headers.append("Authorization", Config.token);
headers.append("CenterId", Config.CenterId);
var options = new RequestOptions({ headers: headers });
var request = {
  url: Config.putImage + id + "/upload",
  method: "POST",
  headers: {
    "Authorization": Config.token,
    "X-Center-Id": Config.XCenterId,
    "Content-Type": "application/octet-stream",
    "File-Name": imageName
  },

  description: "{ 'uploading': " + imageName + " }"
};
var params = [{ name: "image", filename: fileUri, mimeType: 'image/jpeg' }];
var task = session1.multipartUpload(params, request);
task.on("progress", logEvent);
task.on("error", logEvent);
task.on("complete", logEvent);

function logEvent(e) {
  console.log(".........................")
  console.log("currentBytes: " + e.currentBytes);
  console.log(".........................")
  console.log("totalBytes: " + e.totalBytes);
  console.log(".........................")
}

return task;
}

//Extract file Name
extractImageName(fileUri) {
var pattern = /[^/]*$/;
var imageName = fileUri.match(pattern);
return imageName;
}

【讨论】:

  • 感谢 Dlucidone。我的服务器使用 PATCH 方法,正文作为格式数据,键为“profile_image”:“img_name.png”。我的问题是如何将这个主体传递给上面的代码?
  • 使用方法:“PATCH”和“文件名”:img_name”并传递给->这个函数uploadImage(fileUri, method_name, oranything)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-07
  • 1970-01-01
  • 2018-05-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多