Instance Method


Creates a task that performs an HTTP request for uploading the specified file, then calls a handler upon completion.


- (NSURLSessionUploadTask *)uploadTaskWithRequest:(NSURLRequest *)request fromFile:(NSURL *)fileURL completionHandler:(void (^)(NSData *data, NSURLResponse *response, NSError *error))completionHandler;



An NSURLRequest instance that provides the URL, cache policy, request type, and so on. The body stream and body data in this request object are ignored.


The URL of the file to upload.


The completion handler to call when the load request is complete. This handler is executed on the delegate queue.

If you pass nil, only the session delegate methods are called when the task completes, making this method equivalent to the uploadTaskWithRequest:fromFile: method.

This completion handler takes the following parameters:


The data returned by the server.


An object that provides response metadata, such as HTTP headers and status code. If you are making an HTTP or HTTPS request, the returned object is actually an NSHTTPURLResponse object.


An error object that indicates why the request failed, or nil if the request was successful.

Return Value

The new session upload task.


An HTTP upload request is any request that contains a request body, such as a POST or PUT request. Upload tasks require you to create a request object so that you can provide metadata for the upload, like HTTP request headers.

Unlike uploadTaskWithRequest:fromFile:, this method returns the response body after it has been received in full, and does not require you to write a custom delegate to obtain the response body.

By using a completion handler, the task bypasses calls to delegate methods for response and data delivery, and instead provides any resulting data, response, or error inside the completion handler. Delegate methods for handling authentication challenges, however, are still called.

Typically you usually pass a nil completion handler only when creating tasks in sessions whose delegates include a URLSession:dataTask:didReceiveData: method. However, if you do not need the response data, use key-value observing to watch for changes to the task’s status to determine when it completes.

After you create the task, you must start it by calling its resume method.

If the request completes successfully, the data parameter of the completion handler block contains the resource data, and the error parameter is nil. If the request fails, the data parameter is nil, and the error parameter contains information about the failure. If a response from the server is received, regardless of whether the request completes successfully or fails, the response parameter contains that information.

See Also

Adding Upload Tasks to a Session

- uploadTaskWithRequest:fromData:

Creates a task that performs an HTTP request for the specified URL request object and uploads the provided data.

- uploadTaskWithRequest:fromData:completionHandler:

Creates a task that performs an HTTP request for the specified URL request object, uploads the provided data, and calls a handler upon completion.

- uploadTaskWithRequest:fromFile:

Creates a task that performs an HTTP request for uploading the specified file.

- uploadTaskWithStreamedRequest:

Creates a task that performs an HTTP request for uploading data based on the specified URL request.


A URL session task that uploads data to the network in a request body.


A protocol defining methods that URL session instances call on their delegates to handle task-level events specific to data and upload tasks.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software