1

I try to get the filesize of a file stored on a server.

I manage to do, but it takes as long as I would download it.

I assume it will download the file to evaluate the size.

Is there a faster way to get the file size?

func getFileSizeFromURL(sURL:String) -> String{

    let urlPath: String = sURL;
    var url: NSURL = NSURL(string: urlPath)!
    var request1: NSURLRequest = NSURLRequest(URL: url)


    var response : NSURLResponse?
    NSURLConnection.sendSynchronousRequest(request1, returningResponse: &response , error: nil)


    var size:String="";

    if let httpResponse = response as? NSHTTPURLResponse {
        println(httpResponse.expectedContentLength)


        size="\(httpResponse.expectedContentLength)"

    }

    return size;

}
0

1 Answer 1

1

I assume it will download the file to evaluate the size.

Yes, that's because you actually are downloading the file. You should instead use a HEAD request to get just the data about the file. Changing the method from GET or POST to HEAD will get you just the HTTP headers. Insert a line like this after you create request1:

request1.HTTPMethod = "HEAD"
1
  • 1
    That helped. Thanks a lot .
    – mcfly soft
    Oct 29, 2015 at 18:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.