File Upload POST format?

I'm trying to figure out what the files parameter should contain when uploading a file from a windows machine using the api...

I'm trying it with Postman to sort out the syntax..

I have this file name:
C:\Users\Temp\Documents\Business\Test excel spreadsheet.xls

When I use:

@C:\Users\Temp\Documents\Business\Test excel spreadsheet.xls

I get:

{
    "errors": [
        {
            "message": "Please select a file to upload."
        }
    ]
}

So, any suggestions? I thought it was the spaces that may have been causing issues, but I tried a file without one, and it also give the same error..

Thanks

Julian

Dear Greg, thanks for your reply. I still don't get how to use the api anyway ☹️. Wish that someone can guided me with an example of how to use the api..... I also don't quite understand the api call with below url.

https://api.knack.com/v1/pages/scene_key/views/view_key/records

as from the documentation, the scene_key need to change to scene_252 in my case, and view_key need to be view_1625, so I need to write a javascript program in my site to call the value ? is that correct? I just can't call direct from browser right?

Sorry for my stupid questions again, I really don't have knowledge in javascript, I can only write a simple C# program.

Hi Kevin,

The assets URL is pointing to your S3 storage and not an API endpoint - it's not going to return anything without a complete file URL, which you get back from the API after you post a new file.

The ajax script can be in your API & Code > JavaScript section or in a custom script file loaded on your site.

G

I have 2 questions here......

1st....

I use my browser for "https://api.knack.com/v1/applications/{YOUR-APP-ID}/assets/" I get this error"

Cannot GET /v1/applications/5c5befdd0c5efd2cb80d6ff2/assets/"

2nd....
$.ajax({
   url: "https://api.knack.com/v1/pages/{SCENE}/views/{VIEW}/records/",
   type: "POST",
   data: {
      "field_123":"FILE_ID"
   },
   headers: {...},
   success: function (data) {
      // do something with new record data
   },
   error: function (xhr, ajaxOptions, thrownError) {
      // error
   }
});

all the codes above need to put in the knack api&code? sorry for my stupid questions as I'm very new in knack api. thanks for entertaining my questions.

Hi Stephen,

After the file is uploaded, use the "id" field from the returned data for the new file and send a POST request to the data object containing the file connection field to insert a record for the new file:

$.ajax({
   url: "https://api.knack.com/v1/pages/{SCENE}/views/{VIEW}/records/",
   type: "POST",
   data: {
      "field_123":"FILE_ID"
   },
   headers: {...},
   success: function (data) {
      // do something with new record data
   },
   error: function (xhr, ajaxOptions, thrownError) {
      // error
   }
});

Hope this helps.

Hi Greg,
I've got step one (uploading the file) working, but I'm struggling with step two, which is creating a new record in the object, and referencing the file ID. Do you have an example snippet?

Your S3 bucket for the project.  https://api.knack.com/v1/applications/{YOUR-APP-ID}/assets/

what is ASSETS_URL

Not sure what your using, but I have the following AJAX settings for JavaScript API file uploads...works fine.

...
var file = document.getElementById(FILE_INPUT_ID).files[0];
var form = new FormData();
form.append("files", file);

var settings = {
  "async": true,
  "crossDomain": true,
  "url": ASSETS_URL + "file/upload",  // change 'file' to 'image' if uploading images
  "method": "POST",
  "headers": KNACK_HEADERS,
  "processData": false,
  "contentType": false,
  "mimeType": "multipart/form-data",
  "data": form,
  "success": function (data) {
     //..
  },
  "error": function(response) {
     //...
 }
}

$.ajax(settings).done(function (response) {  // returns JSON with file details
   //...do something with response
});
...