Backendless Support
 
Works as Designed

I would like to change uploaded txt to an mp3 or wave file in the cloud

HI,

I currently upload a txt data file to backndless for each user. Is it possible to convert this to a wave file and then save it in the cloud? or do I need to upload a new file?

Best,

F

Leave a Comment

Comments (13)

photo
1

ok what if I want to upload a data file as a wave file?

My current code is :

  1. class func uploadData(data: [Float]) -> Promise<BackendlessFile> {

    let rawData = String(describing: data).data(using: .utf8)!

    let fileName = "\(currentUser.firstName)-\(currentUser.lastName)-\(Date().timeIntervalSince1970)"

    return Promise { (fulfill, reject) in

    backendless.fileService.upload("screams/\(fileName).txt",

    content: rawData,

    response: { file in

    fulfill(file!)

    },

    error: { fault in

    reject(fault!)

    })

    }

    }

photo
1

Why would you want to do that? I'm not sure you can convert wave file to text and then back without data corruption.

photo
1

I wont be converting it back...if I can upload it as a wave file I wont change it again

photo
2

You can upload it as pure byte array with a name 'yourfile.wav'. You don't need to convert it to string. Just use the saveFile method: https://backendless.com/docs/js/doc.html#file_save_files_from_byte_arrays

photo
1

Thank you but thats javascript, Is there a similar function in swift?

photo
1

Thank you. I have the following code. but the uploaded file is not playable:

  1. class func uploadData(data: [Float]) -> Promise<BackendlessFile> {

    let rawData = NSData(bytes: data, length: data.count*MemoryLayout<Float>.size)

    //String(describing: data).data(using: .utf8)!

    let fileName = "\(currentUser.firstName)-\(currentUser.lastName)-\(Date().year)-\(Date().month)-\(Date().day)"

    return Promise { (fulfill, reject) in

    backendless.fileService.saveFile("screams/\(fileName).wav",

    content: rawData as Data!,

    response: { file in

    fulfill(file!)

    },

    error: { fault in

    reject(fault!)

    })

    }

    }

photo
1

I suppose you are implying that the bytes were not saved correctly.

Could you verify that the file created as a result of the upload contains exactly the same bytes you send in the API call?

Regards,

Mark

photo
1

Hi Mark,

Not exactly. I don't know much about audio encoding. I changed the code to:

  1. class func uploadData(data: [Float]) -> Promise<BackendlessFile> {
  2. let rawData = String(describing: data).data(using: .utf8)!
  3. let fileName = "\(currentUser.firstName)-\(currentUser.lastName)-\(Date().year)-\(Date().month)-\(Date().day)"
  4. return Promise { (fulfill, reject) in
  5. backendless.fileService.saveFile("screams/\(fileName).txt",
  6. content: rawData,
  7. response: { file in
  8. fulfill(file!)
  9. },
  10. error: { fault in
  11. reject(fault!)
  12. })
  13. }
  14. }

...this uploads a txt file with an array of numbers as expected. Are you sure we can just change the name of the file?

Best,

Feras A.

photo
1

Hi Feras,

Backendless saves the bytes you send the same way whether you specify .txt or .wav. The code which persists the files does not apply any kind of different encoding.

If the file does not play, the first thing you should do is to check whether the saved bytes are the same as the ones you sent. Any HEX comparison tool should be able to help you with that.

IF the content is the same AND the file does not play, then the problem is with the bytes you sent. Otherwise, if the content is different, please let us know and we will be happy to investigate why it was not the persisted as expected.

Regards,

Mark

photo
1

I believe the content is the same. therefore I am preparing it in the wrong manner.

Thanks

F

photo
1

Thanks for letting us know. I will close this topic with the resolution of "Works as designed". Please let us know if we can be of any help.

Regards,

Mark

photo