3

Always when I post a new media file the filename fildset in mongo is become null.

I've used curl, postman and python requests library but always result is the same :(

The definition in the settings.py are:

EXTENDED_MEDIA_INFO = ['content_type', 'name', 'length']

(I've tried with "filename" too)

And the definition:

files = { 
    'item_title': 'file',
    'auth_field': 'account_id',
    'schema': {
        'title': {
            'type': 'string',
            },  
        'account_id': {
            'type': 'objectid',
            'required': True,
            'data_relation': {
                'resource': 'accounts',
                'field': '_id',
                }   
            },  
        'file': {
            'type': 'media',
            'required': True,
            },  
        'tags': {
            'type': 'list',
            'schema': {
                'type': 'string',
                },  
            },

        }   
    }

When I use curl as in your web example:

curl -F "account_id=553a0b0f8a6f501695fa8b42" -F "file=@blua.jpg" http://localhost:5000/files

{"_updated": "Thu, 30 Apr 2015 21:47:51 GMT", "_links": {"self": {"href": "files/5542a3078a6f5064a3a04dba", "title": "file"}}, "_created": "Thu, 30 Apr 2015 21:47:51 GMT", "_status": "OK", "_id": "5542a3078a6f5064a3a04dba", "_etag": "90314f9491a92dc84aa90880d69e2b76bfd947db"}

In mongo is stored:

> db.fs.files.find({}).pretty()
{
    "_id" : ObjectId("5542a54f8a6f5064a3a04dbb"),
    "contentType" : "image/jpeg",
    "chunkSize" : 261120,
    "filename" : null,                   <-------------------- THE PROBLEM
    "length" : 437658,
    "uploadDate" : ISODate("2015-04-30T21:57:35.080Z"),
    "md5" : "90dce7915217108434acd61b3285d907"
}
> db.files.find({}).pretty()
{
    "_id" : ObjectId("5542a54f8a6f5064a3a04dbe"),
    "_updated" : ISODate("2015-04-30T21:57:35Z"),
    "account_id" : ObjectId("553a0b0f8a6f501695fa8b42"),
    "file" : ObjectId("5542a54f8a6f5064a3a04dbb"),
    "_created" : ISODate("2015-04-30T21:57:35Z"),
    "_etag" : "466ab27d09d6eb87d2c0ab5f094875011e72cca2"
}

Using requets python [http://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file]:

def file_post(token, account_id, filename, content, mimetype, debug=True):
  path = __path__
  url = Bkend.url(path)
  headers = {'Authorization': Rest.authorization(token)}

  payload = {'account_id': account_id}

  resp = requests.post(
      url=url,
      headers=headers,
      data=payload,
      files={'file': (secure_filename(filename), content, mimetype)}
     #files={'file': ('filename', content, mimetype)}
     #files={'file': content}
      )

  if debug:
      log = "\n - Url: " + str(url) + \ 
          "\n - Headers:" + str(headers) + \ 
          "\n - Payload/Params:" + json.dumps(payload) + \ 
          "\n\n...............................\n" + \ 
          "\n - Resp Status Code: " + str(resp.status_code) + \ 
          "\n - Resp URL: " + str(resp.url) + \ 
          "\n - Resp Headers: " + str(resp.headers) + \ 
          "\n - Resp Avg: " + str(resp.elapsed) + \ 
          "\n - Resp Body: \n" + resp.text
      capp.logger.debug(log)
  return resp

I've even tested to set the filename, content_type and headers explicitly, but always get the same result

> db.files.find({}).pretty()
{
    "_id" : ObjectId("5542a86c8a6f5064a3a04dce"),
    "_updated" : ISODate("2015-04-30T22:10:52Z"),
    "account_id" : ObjectId("553a0b0f8a6f501695fa8b42"),
    "file" : ObjectId("5542a86c8a6f5064a3a04dbf"),
    "_created" : ISODate("2015-04-30T22:10:52Z"),
    "_etag" : "d5a203b20dc75a8ae8691cd8cfecb627da9a181b",
    "tags" : [
        "STOP",
        "BUS",
        "MELON"
     ],
    "title" : "The bus stop" 
}
> db.fs.files.find({}).pretty()
{
    "_id" : ObjectId("5542a86c8a6f5064a3a04dbf"),
    "contentType" : "application/pdf",
    "chunkSize" : 261120,
    "filename" : null,         <-------------- THE PROBLEM
    "length" : 3589703,
    "uploadDate" : ISODate("2015-04-30T22:10:52.507Z"),
    "md5" : "f2176c1780b32b549ca7513893254c06"
}
> 

With Postman: screenshot - 010515 - 00 19 43

The result in mongo with the same end:

> db.files.find({}).pretty()
{
    "_id" : ObjectId("5542aa768a6f5064a3a04de1"),
    "_updated" : ISODate("2015-04-30T22:19:34Z"),
    "account_id" : ObjectId("553a0b0f8a6f501695fa8b42"),
    "file" : ObjectId("5542aa768a6f5064a3a04ddf"),
    "_created" : ISODate("2015-04-30T22:19:34Z"),
    "_etag" : "d6a3ba5ad5f6fb7ef6557000841194501815e39a"
}
> db.fs.files.find({}).pretty()
{
    "_id" : ObjectId("5542aa768a6f5064a3a04ddf"),
    "contentType" : "application/pdf",
    "chunkSize" : 261120,
    "filename" : null,      <------ The problem again
    "length" : 16651,
    "uploadDate" : ISODate("2015-04-30T22:19:34.269Z"),
    "md5" : "20ef57d98d4bc11ab55a260a4d75b727"
}
> 

Any idea? is a bug or I'am doing something wrong?

Solved fails in versión 0.5.3 and is fixed in version 0.5.4

Anihur
  • 78
  • 4
  • Your server probably has to name the file other wise you could overwrite anything on the server(that the server prosess had accsess to) – mjz19910 Apr 30 '15 at 22:48
  • at http://stackoverflow.com/questions/12763913/upload-files-on-remote-server-through-html-javascript @Erwin says "You need to have server-side code to save your file," – mjz19910 Apr 30 '15 at 22:54
  • The filename is an atribute of the file (its came with it). You can get it as the mimetype (file.filename atribute and its arrived well) However, i tried to force it and fails too – Anihur Apr 30 '15 at 22:56
  • I think this is a dupilcate of question id:12763913 although it is in a different language you should be able to translate any fourm posts to any other language – mjz19910 Apr 30 '15 at 22:56
  • It is also not possible to save a file with a filename; that is sent with http form posts – mjz19910 Apr 30 '15 at 22:58
  • your SERVER MUST deal with the filename you can't post files with their file names and expect the server to automatically save it with that name(unless you program your server to do so) – mjz19910 Apr 30 '15 at 23:02
  • I don't have problem to save the files! If you look the explanation... the problem is that always put the filename a null, but the content and the mimetype are correct! I don't think is the same than question id:12763913 as you said – Anihur Apr 30 '15 at 23:04
  • Moreover I recibed the name in the server and I force it as you can see that is posible in a post call -> Using requets python [http://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file] – Anihur Apr 30 '15 at 23:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/76681/discussion-between-mjz19910-and-anihur). – mjz19910 Apr 30 '15 at 23:10
  • In a post multipart/form-data the filename is included as equal than mimetye – Anihur Apr 30 '15 at 23:11
  • Unlike forum sites, we don't use "Thanks", or "Any help appreciated", or signatures on [so]. See "[Should 'Hi', 'thanks,' taglines, and salutations be removed from posts?](http://meta.stackexchange.com/questions/2950/should-hi-thanks-taglines-and-salutations-be-removed-from-posts). BTW, it's "Thanks in advance", not "Thanks in advantage". That doesn't even make _sense_. – John Saunders May 01 '15 at 04:26

1 Answers1

2

This is probably related to this ticket, which has been closed 13 days ago. Try pulling from Eve 0.5.4, it should solve your problem.

Nicola Iarocci
  • 6,606
  • 1
  • 20
  • 33