0

I am trying to upload a zipfile to the server. When i tried to upload a zip file to the server, File is not getting uploaded successfully. I am getting error from the server. At the server side userid is separated from the file name.

The actual server JSON response is:

{"error":{"login":"Sorry,Failed to Login "}}

I am new to multipart/form-data, so I don't know the cause behind this issue. This is my code:

func uploadData() {
    let headers: HTTPHeaders = [
        "Content-type": "multipart/form-data"
    ]

    var username:String = username_textfield.text!
    var password:String = password_textfield.text!

    let fileManager = FileManager()

    let paths = (NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as NSString).appendingPathComponent("/cropsapdb_up_\(useridsaved).zip")
    var filepath = paths

    var sourceURL = URL(fileURLWithPath: filepath)
    var zipnewdata:NSData = NSData()

    zipnewdata = FileManager.default.contents(atPath: filepath) as! NSData

    let parameters = ["unm2": username,"pass2":password,"ufile":filepath]

    Alamofire.upload(multipartFormData: { multipartFormData in
        multipartFormData.append( zipData, withName: "cropsapdb_up_\(self.useridsaved)", fileName: "cropsapdb_up_\(self.useridsaved).zip", mimeType: "application/zip")
        for (key, value) in parameters {
            multipartFormData.append((value.data(using: String.Encoding.utf8)!), withName: key)
        }
    }, usingThreshold: UInt64.init(), to: "myurl", method: .post, headers: headers) { (result) in
        switch result{
        case .success(let upload, _, _):
            upload.responseJSON { response in
                print("Succesfully uploaded = \(response.data)")
                if let JSON = response.result.value
                {
                    print("JSON: \(JSON)")
                }
                if let err = response.error{
                    print("response error")
                    return
                }

            }
        case .failure(let error):
            print("Error in upload: \(error.localizedDescription)")

        }
    }
}
Rob
  • 415,655
  • 72
  • 787
  • 1,044
Abhi
  • 95
  • 8
  • Are you sure the field name of the upload is correct? Many web services are expected some fixed, predefined field name for the upload, but you're building something dynamically from the userid. – Rob Mar 11 '18 at 18:29
  • Yes , my field names are correct. – Abhi Mar 11 '18 at 18:39
  • Actually my request is going to server and I am getting error from the server. At the server side userid is separated from the file name. But my file is not uploading to server that's why I am getting error. – Abhi Mar 11 '18 at 18:43
  • "Userid is separated from the file name"... Precisely which field has them combined, but you need them separated? What looks suspicious to me is the second parameter to `multipartFormData.append`, the `withName`, which combines filename and userid, but you said that wasn't the issue. So precisely which _is_ the issue? – Rob Mar 11 '18 at 18:46
  • Unrelated to your question at hand, if you wanted to tidy this up a bit, I might suggest https://gist.github.com/robertmryan/f5418683bf6fc04852f6cfbdf063c3d8. – Rob Mar 11 '18 at 18:48
  • It's actually not that easy to do multipart/form-data uploads manually, but I think [this answer](https://stackoverflow.com/a/48120313/1986221) could be useful (check the "Video Uploading" part, it should be clarifying). – Alejandro Iván Mar 11 '18 at 19:06
  • I am not getting error, which means I didn't do anything wrong in coding. But my file is not uploading. That's why in response i am getting error. – Abhi Mar 11 '18 at 19:07
  • “I am not getting error” ... sure, you’re not getting compiler error, but you say you’re getting server error. Perhaps you can edit your question and share the exact wording of the server error. Or, as as a last resort, maybe you can share the server code that parses this request and we might be able to reverse engineer what the problem is. But it’s impossible to diagnose on the basis of the information provided thus far. – Rob Mar 11 '18 at 19:11
  • this is the response I am getting from server :{"error":{"login":"Sorry,Failed to Login "}} – Abhi Mar 11 '18 at 19:24
  • Thanks for sharing that. That response suggests that either (a) the userid or password values are wrong; (b) the keys for those two parameters are wrong; or (c) the server is trying to authenticate you using some completely different way. Again, it’s hard to say which it is on the basis of the information provided. Can you share any details about how the server is authenticating your request? – Rob Mar 11 '18 at 19:36
  • can you please give me your mail id I will mail you. My key and values are correct. – Abhi Mar 11 '18 at 19:40
  • I’m having trouble reconciling your comment about the problem about “Userid is separated from the file name” and the error, which suggests a more fundamental authentication problem. – Rob Mar 11 '18 at 19:41
  • I’d rather not post my email address, but my Stack Overflow profile includes my home page, which has link for sending me email. – Rob Mar 11 '18 at 19:45
  • filename consists of userid. example: filename_248. last three digit is userid which is used during login. But my file is not uploading to server that's why I am getting error during login. – Abhi Mar 11 '18 at 19:48
  • ok. I will mail you server side code. And thanks. – Abhi Mar 11 '18 at 19:51
  • BTW, I find it perfectly plausible that the file name is `filename_248.zip`. What I'm suspicious about is that you're also saying that the _field_ name is `filename_248`. So you have server code that handles dynamically generated field names as well as unique file names? One can do that, but it's atypical. When I see the server code (which I have not yet received), it will become more clear. – Rob Mar 12 '18 at 01:29
  • I got it. I was passing wrong "withname:" . Now its working. – Abhi Mar 13 '18 at 10:03

0 Answers0