-1

I am making an app which behaves like cloud storage. I need to upload a file with Alamofire. When I am uploading the related file, I need to append a string which holds path like: "folderName/fileName" (which shows the path on my cloud storage app) to my file data in bytes. I can achieve this by using below code in Java:

@Override
protected Map<String, DataPart> getByteData() {
   Map<String, DataPart> params = new HashMap<>();

   params.put("parameterName", new DataPart("folderName/fileName", fileInBytes));
   return params;
}

But I don't know how to do this in Swift. I need to create a new Data with a string and a data, before uploading it with Alamofire. How can I achieve that? Any response will be appreciated.

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Nilay Dagdemir
  • 236
  • 2
  • 15

2 Answers2

0

Thanks everyone who answered me, I've found a solution. I created a structure called DataPart with a string and data, and put that parameter to my Alamofire request with parameter name. "What is the equivalent of a Java HashMap<String,Integer> in Swift" this entry helped me a lot in case you are wondering.

Nilay Dagdemir
  • 236
  • 2
  • 15
-1

If I understand correctly, you want to convert your path (of type String) to swift's Data structure, then upload it to your server alongside a joined file's data.

so here's how you can do it-

let path = "folderName/fileName"
let pathEncodedData = path.data(using: .utf8)!

let fileData: Data = #YourFile'sData

let params = ["path": pathEncodedData, "fileData": fileData]
Niv
  • 517
  • 5
  • 18