---
title: "Redirect på nå vis"
type: "forum-thread"
url: "https://www.webforum.nu/amne/javascript/192253-redirect-på-nå-vis"
topic: "JavaScript"
topic_url: "https://www.webforum.nu/amne/javascript"
author: "Asa"
published: "2014-04-15T12:40:34.000Z"
updated: "2014-04-24T07:02:23.000Z"
replies: 9
views: 1379
page: 1
pages: 1
language: "sv-SE"
site: "webForum — webforum.nu"
rights: "Upphovsrätten till varje inlägg tillhör dess författare."
attribution: "Citera som: webForum, https://www.webforum.nu/amne/javascript/192253-redirect-på-nå-vis"
---

# Redirect på nå vis

## #1 — Asa, 2014-04-15T12:40Z

Har denna koden som anropas efter en upload av filer:

```
$(function(){

    var ul = $('#upload ul');

    $('#drop a').click(function(){
        // Simulate a click on the file input button
        // to show the file browser dialog
        $(this).parent().find('input').click();
    });

    // Initialize the jQuery File Upload plugin
    $('#upload').fileupload({

        // This element will accept file drag/drop uploading
        dropZone: $('#drop'),

        // This function is called when a file is added to the queue;
        // either via the browse button, or via drag/drop:
        add: function (e, data) {

            var tpl = $('<li class="working"><input type="text" value="0" data-width="48" data-height="48"'+
                ' data-fgColor="#0788a5" data-readOnly="1" data-bgColor="#3e4043" /><p></p><span></span></li>');

            // Append the file name and file size
            tpl.find('p').text(data.files[0].name)
                         .append('<i>' + formatFileSize(data.files[0].size) + '</i>');

            // Add the HTML to the UL element
            data.context = tpl.appendTo(ul);

            // Initialize the knob plugin
            tpl.find('input').knob();

            // Listen for clicks on the cancel icon
            tpl.find('span').click(function(){

                if(tpl.hasClass('working')){
                    jqXHR.abort();
                }

                tpl.fadeOut(function(){
                    tpl.remove();
                });

            });

            // Automatically upload the file once it is added to the queue
            var jqXHR = data.submit();
        },

        progress: function(e, data){

            // Calculate the completion percentage of the upload
            var progress = parseInt(data.loaded / data.total * 100, 10);

            // Update the hidden input field and trigger a change
            // so that the jQuery knob plugin knows to update the dial
            data.context.find('input').val(progress).change();

            if(progress == 100){
                data.context.removeClass('working');
            }
        },

        fail:function(e, data){
            // Something has gone wrong!
            data.context.addClass('error');
        }

    });

    // Prevent the default action when a file is dropped on the window
    $(document).on('drop dragover', function (e) {
        e.preventDefault();
    });

    // Helper function that formats the file sizes
    function formatFileSize(bytes) {
        if (typeof bytes !== 'number') {
            return '';
        }

        if (bytes >= 1000000000) {
            return (bytes / 1000000000).toFixed(2) + ' GB';
        }

        if (bytes >= 1000000) {
            return (bytes / 1000000).toFixed(2) + ' MB';
        }

        return (bytes / 1000).toFixed(2) + ' KB';
    }

});
```

Nu vill jag att den ska skicka en vidare till en sida efter upload. Som det är nu är man kvar på uploadsidan. Man skall helst skickas vidare till en sida som heter photos.php?albumID=X där x på något vis skall komma med.

Skickar med variabeln album_id som hidden i formuläret där man väljer vilka filer man skall ladda upp.
Samt även hela länken som man ska skicka till efteråt. När man väl vet hur man gör. Ligger i variebeln backURL

Se exemplet här hur det är:
<http://demo.tutorialzine.com/2013/05/mini-ajax-file-upload-form/>

Permalänk: https://www.webforum.nu/p/192253

## #2 — Asa, 2014-04-15T12:47Z

Såg detta i en annan fil:

```
            // Set the following option to the location of a redirect url on the
            // origin server, for cross-domain iframe transport uploads:
            redirect: undefined,
            // The parameter name for the redirect url, sent as part of the form
            // data and set to 'redirect' if this option is empty:
            redirectParamName: undefined,
```

Länk till hela filen: <https://github.com/blueimp/jQuery-File-Upload/blob/master/js/jquery.fileupload.js>

Men vete tusan hur de menar..

Wiki info

```
redirect

Set this option to the location of a redirect url on the origin server (the server that hosts the file upload form), for cross-domain iframe transport uploads. If set, this value is sent as part of the form data to the upload server.
The upload server is supposed to redirect the browser to this url after the upload completes and append the upload information as URL-encoded JSON string to the redirect URL, e.g. by replacing the "%s" character sequence.

Type: string
Example: 'http://example.org/cors/result.html?%s'
redirectParamName

The parameter name for the redirect url, sent as part of the form data and set to 'redirect' if this option is empty.

Type: string
Example: 'redirect-url'
```

Snälla hjälp mig. SKa man skriva nåt på båda och vad menar de med **?%s** ?

Har testat skriva länkar omfamnade av '' i de exemplen som är som de visar men inget händer efter upload.

Permalänk: https://www.webforum.nu/p/2315955

## #3 — Asa, 2014-04-16T21:31Z

Ingen som vet ? jojoxx??

Permalänk: https://www.webforum.nu/p/2315968

## #4 — Jojoxx, 2014-04-17T06:45Z

> **[Asa skrev:](https://www.webforum.nu/p/1570930)**
>
> Ingen som vet ? jojoxx??

Har inte haft tid att läsa igenom wall-of-code, men du har ett block som ser ut så här:

```
            if(progress == 100){
                data.context.removeClass('working');
            }
```

Så ett hett tips är väl att göra det du vill göra när uppladdningen är klar där. (location.href=....)

Permalänk: https://www.webforum.nu/p/2315969

## #5 — Nate.A, 2014-04-17T07:06Z

```
    // Initialize the jQuery File Upload plugin
    $('#upload').fileupload({
        /* Klippte bort din kod här för att det skulle bli lättare att se.. */
        done:function(e, data){
                /* Gör din redirect här */
        }
    });
```

I dokumentationen ser det ut som det finns en metod du kan använda dig av för att köra en funktion när uppladdningen är klar.
(rad 119 i exemplet: <https://github.com/blueimp/jQuery-File-Upload/blob/master/basic.html#L119>)

Permalänk: https://www.webforum.nu/p/2315971

## #6 — Asa, 2014-04-17T07:07Z

Tackar allesammans :)

Permalänk: https://www.webforum.nu/p/2315972

## #7 — Asa, 2014-04-21T09:27Z

Verkade inte fungera :/

Permalänk: https://www.webforum.nu/p/2316010

## #8 — voigtann1, 2014-04-21T11:18Z

> **[Asa skrev:](https://www.webforum.nu/p/1570982)**
>
> Verkade inte fungera :/

hur ser din nuvarande kod som inte fungerade?

Permalänk: https://www.webforum.nu/p/2316012

## #9 — Asa, 2014-04-21T19:53Z

```
$(function(){

    var ul = $('#upload ul');

    $('#drop a').click(function(){
        // Simulate a click on the file input button
        // to show the file browser dialog
        $(this).parent().find('input').click();
    });

    // Initialize the jQuery File Upload plugin
    $('#upload').fileupload({

        // This element will accept file drag/drop uploading
        dropZone: $('#drop'),

        // This function is called when a file is added to the queue;
        // either via the browse button, or via drag/drop:
        add: function (e, data) {

            var tpl = $('<li class="working"><input type="text" value="0" data-width="48" data-height="48"'+
                ' data-fgColor="#0788a5" data-readOnly="1" data-bgColor="#3e4043" /><p></p><span></span></li>');

            // Append the file name and file size
            tpl.find('p').text(data.files[0].name)
                         .append('<i>' + formatFileSize(data.files[0].size) + '</i>');

            // Add the HTML to the UL element
            data.context = tpl.appendTo(ul);

            // Initialize the knob plugin
            tpl.find('input').knob();

            // Listen for clicks on the cancel icon
            tpl.find('span').click(function(){

                if(tpl.hasClass('working')){
                    jqXHR.abort();
                }

                tpl.fadeOut(function(){
                    tpl.remove();
                });

            });

            // Automatically upload the file once it is added to the queue
            var jqXHR = data.submit();
        },

        progress: function(e, data){

            // Calculate the completion percentage of the upload
            var progress = parseInt(data.loaded / data.total * 100, 10);

            // Update the hidden input field and trigger a change
            // so that the jQuery knob plugin knows to update the dial
            data.context.find('input').val(progress).change();

            if(progress == 100){
                data.context.removeClass('working');
            }
        },

        fail:function(e, data){
            // Something has gone wrong!
            data.context.addClass('error');
        }
        
        done:function(e, data){
            window.location.href='http://www.sidan.com/';
        }

    });

    // Prevent the default action when a file is dropped on the window
    $(document).on('drop dragover', function (e) {
        e.preventDefault();
    });

    // Helper function that formats the file sizes
    function formatFileSize(bytes) {
        if (typeof bytes !== 'number') {
            return '';
        }

        if (bytes >= 1000000000) {
            return (bytes / 1000000000).toFixed(2) + ' GB';
        }

        if (bytes >= 1000000) {
            return (bytes / 1000000).toFixed(2) + ' MB';
        }

        return (bytes / 1000).toFixed(2) + ' KB';
    }

});
```

        done:function(e, data){
            window.location.href='http://www.sidan.com/';
        }

Permalänk: https://www.webforum.nu/p/2316015

## #10 — Jojoxx, 2014-04-24T07:02Z

Jag ser att det är ett syntax-fel på raden innan. Det saknas ett komma-tecken mellan fail och done-funktionerna:

```
        fail:function(e, data){
            // Something has gone wrong!
            data.context.addClass('error');
        },
        done:function(e, data){
            window.location.href='http://www.sidan.com/';
        }
```

Hett tips: Kolla loggen så ser du sådana syntax-fel :)

Permalänk: https://www.webforum.nu/p/2316031

---

Tråden på webben: https://www.webforum.nu/amne/javascript/192253-redirect-på-nå-vis
