             defered.reject(response)
             return defered.promise
 
-        xhr = new XMLHttpRequest()
-
         uploadProgress = (evt) =>
             $rootScope.$apply =>
                 file.status = "in-progress"

        uploadComplete = (evt) =>
            $rootScope.$apply ->
                file.status = "done"
                try
                    data = JSON.parse(evt.target.responseText)
                catch
                    data = {}

                model =  $model.make_model(urlName, data)
                if xhr.status >= 200 and xhr.status < 400
                    defered.resolve(model)
                else
                    response = {
                        status: xhr.status,
                        data: {_error_message: JSON.parse(xhr.response)['attached_file']?[0]}
                    }
                    defered.reject(response)


        data.append("object_id", objectId)
        data.append("attached_file", file)

        xhr = new XMLHttpRequest()
        xhr.upload.addEventListener("progress", uploadProgress, false)
        xhr.addEventListener("load", uploadComplete, false)
        xhr.addEventListener("error", uploadFailed, false)
