简体   繁体   English

为什么我的图像文件被转换为字符串?

[英]Why is my image file being converted to a string?

Edit, original question has been resolved as to why file was converted to string. 编辑,原来的问题已解决,为什么文件转换为字符串。 The code has been edited to reflect these corrections. 该代码已被编辑以反映这些更正。 The API handler is now outputting object as data type and buffer as the value of request.payload.file. API处理程序现在将对象输出为数据类型,并将缓冲区输出为request.payload.file的值。

I'm using Aurelia to make a Single Page App. 我正在使用Aurelia制作单页应用程序。 There is a HTML form that accepts two input fields, one for a file, one for text. 有一个HTML表单可以接受两个输入字段,一个输入域用于文件,一个输入域用于文本。 These fields are bound to variables (selecetdImage and title) in an associated TypeScript view model. 这些字段绑定到关联的TypeScript视图模型中的变量(selecetdImage和title)。 In the viewmodel they are used as arguments in a function that appends them to formData and sends a http post request with the formData to an Node/js Hapi framework API handler. 在viewmodel中,它们用作函数的参数,该函数将其附加到formData并将带有formData的http发布请求发送到Node / js Hapi框架API处理程序。

When I console.log(typeof(selectedImage) in the Aurelia app, it states object, but when I console log typeOf(selecetdImage) in the handler, I get String. I'm guessing this is why my function isn't working and giving 500 error messages 当我在Aurelia应用程序中console.log(typeof(selectedImage)时,它声明对象,但是当我在处理程序中控制台log typeOf(selecetdImage)时,我得到了String。我猜测这就是为什么我的函数不起作用并且给出500条错误消息

The handler itself works. 处理程序本身可以工作。 I used it in a MVC server based web app. 我在基于MVC服务器的Web应用程序中使用了它。 In that context, HTML form triggers a post request, and the MVC handler successfully receives the file, writes it to local/temp.img and uploads it to cloudinary. 在这种情况下,HTML表单触发了发布请求,并且MVC处理程序成功接收了文件,将其写入local / temp.img并将其上传到cloudinary。 But with the API version, where I assembled the form data myself as above, the file isn't written to local/temp.img and the cloudinary upload fails. 但是在API版本中,我如上所述自己在其中组装了表单数据,该文件未写入local / temp.img,并且cloudinary上传失败。

Edit. 编辑。 I changed the viewmodel variables to title = null; 我将viewmodel变量更改为title = null; files = null; files = null;

and I changed the formData append function to: 我将formData追加函数更改为:

formData.append('file', File, files[0]); formData.append('file',File,files [0]);

As per the example here. 按照这里的例子。 The code below is now modified to match this update. 现在,下面的代码已被修改以匹配此更新。

Now when I console log the value of file in the API handler, the output is: 现在,当我在API处理程序中控制台记录文件的值时,输出为:

    <Buffer ff d8 ff e0 00 10.......

I'm not sure what to do with this data. 我不确定该如何处理。 I assume it's the image binary data in octal? 我假设它是八进制的图像二进制数据? And if so, does anyone know how to write it as an image in node? 如果是这样,有人知道如何在节点中将其写为映像吗? The payload is no longer of type string, now it's type object. 有效负载不再是字符串类型,现在是对象类型。

      <form submit.delegate="uploadPhoto()" enctype="multipart/form-data">
        <div class="two fields">
          <div class="field">
            <label>File</label>
            <input type="file" name="file" accept="image/png, image/jpeg" files.bind="files">
          </div>
          <div class="field">
            <label>Title</label> <input value.bind="title">
          </div>
        </div>
        <button type="submit"> Upload </button>
      </form>


    //photoUpload.ts// (view model associated with above html

    @inject(POIService)
    export class PhotoForm {

      @bindable
      photos: Photo[];
      title = null;
      files = null;

      constructor (private poi: POIService) {}
      uploadPhoto() {
        const result = this.poi.uploadPhoto(this.title, this.files);
        console.log(this.title);
        console.log(result);
      }  

    //POIService (where contains injected function to create HTTP request

    async uploadPhoto(title: string, files){
        console.log("now uploading photo for: " + this.currentLocation)
        let formData = new FormData();
        formData.append("title", title);
        formData.append("location", this.currentLocation); //don't worry about this variable, it's set by another function every time a page is viewed
        formData.append("file", files[0]);
        const response = await this.httpClient.post('/api/locations/' + this.currentLocation + '/photos', formData);
        console.log(response);
      }

    //API Handler (accepts HTTP request, writes the image to a local folder, the uploads to cloudinary and returns the url and photo_id which are stored in a Mongod document

  create: {
        auth: false,
        handler: async function(request, h) {
          const photo = request.payload.file;
          await writeFile('./public/temp.img', photo);
          const result = await cloudinary.v2.uploader.upload('./public/temp.img', function(error, result) {
            console.log(result)
          });
          const newPhoto = new Photo({
            title: request.payload.title,
            url: result.url,
            public_id: result.public_id,
            location: request.params.name
          })
          await newPhoto.save();
          return newPhoto;
        }
      },

Is it a very long string, containing "data:b64" in the first 15 or so characters? 它是一个很长的字符串,在前15个左右的字符中包含“ data:b64”吗? If so, that means it's the base64 data. 如果是这样,则意味着它是base64数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM