2012-05-07 88 views
3

,所以我使用swfuploader有裝載swfuploader這段JavaScript代碼到頁面(​​)SWFUpload的302錯誤

swfuPubThumbnailUploader = new SWFUpload({ 
    upload_url : "/upload_thumbnail", 
    flash_url : "/Flash/swfupload.swf", 
    file_size_limit : 512 + " MB", 
    file_post_name: 'files[swf]', 
    file_types : '*.jpg;*.jpeg;*.gif;*.png', 
    file_types_description: "Image Files", 
    file_queue_limit : 1, 
    button_placeholder_id: 'swf-trans-file-selector', 
    button_text: '', 
    button_image_url: '', 
    button_width: "85", 
    button_height: "25", 
    button_cursor : SWFUpload.CURSOR.HAND, 
    button_window_mode : SWFUpload.WINDOW_MODE.TRANSPARENT, 

    post_params : { 
       "sid" : sid 
    }, 

    preserve_relative_urls : true, 
    file_queued_handler : fileQueued, 
    file_queue_error_handler : fileQueueError, 
    file_dialog_complete_handler : fileDialogComplete, 
    file_dialog_start_handler : sLibraryFileDialogStart, 
    upload_start_handler : sLibraryPubUploadStart, 
    upload_progress_handler : sLibraryPubUploadProgress, 
    upload_error_handler : sLibraryPubUploadError, 
    upload_success_handler : sLibraryPubUploadSuccess, 
    upload_complete_handler : sLibraryPubUploadComplete, 
    queue_complete_handler : queueComplete, // Queue plugin event 

    custom_settings: { 
    progressTarget : ("fsPPUploadProgress") 
    } 
}); 

但後來每當我上傳(上傳圖片,更具體的...),swfuploader會抱怨302錯誤,這表明該網址正在重定向...但是當我訪問上傳網址'/ upload_thumbnail'時,沒有重定向發生,上傳網址加載正常...所以我檢查了提琴手檢查兩個案件和這裏是每個案件的請求

當我訪問/ upload_thumbnail通過瀏覽器:

GET /upload_thumbnail HTTP/1.1 
Host: my.domain.com 
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Language: en-us,en;q=0.5 
Accept-Encoding: gzip, deflate 
Connection: keep-alive 
Cookie: __utma=171146939.260757561.1311084520.1336078159.1336400574.117; __utmz=171146939.1311084520.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=104299925.1228643063.1313075490.1335542105.1335567483.41; __utmz=104299925.1333981938.38.25.utmcsr=my.domain.com:8090|utmccn=(referral)|utmcmd=referral|utmcct=/browse/MD-2760; __qca=P0-29539114-1325619242917; SESS5ccc5f20cb72179af2f569e77eaa49da=6vde0rln7q3384k5lcvedsgfh3; SimpleSAMLAuthToken=_c2f653a6e9b18b10439d73849fb1142a7d43851998; __utmc=171146939; has_js=1 
If-Modified-Since: Mon, 07 May 2012 19:13:38 +0000 
If-None-Match: "1336418018" 

它加載罰款狀態200

然後從swfuploader

POST /upload_thumbnail HTTP/1.1 
Accept: text/* 
Content-Type: multipart/form-data; boundary=----------gL6Ef1Ef1KM7Ij5gL6ae0gL6GI3GI3 
User-Agent: Shockwave Flash 
Host: my.domain.com 
Content-Length: 847357 
Connection: Keep-Alive 
Pragma: no-cache 
Cookie: __utma=171146939.1934231828.1323362429.1328283903.1333582993.5; __utmz=171146939.1323362429.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); __utma=104299925.1653270920.1323362234.1334760245.1335992862.7; __utmz=104299925.1323362234.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); SESS5ccc5f20cb72179af2f569e77eaa49da=0qjn1gva2rhlcqiqp2g7liumd0; __qca=P0-1855943596-1327334275533 

它結束了與狀態302和重定向到登錄頁面的一個....

所以我發現罪魁禍首是Cookie:請求....如果我將瀏覽器的COOKIE請求切換到swfuploader的Cookie請求,swfuploader請求將正確加載狀態200 ...

我的第一個問題是.....爲什麼? swfuploader的Cookie請求有什麼問題?

其次,這可怎麼翻譯成我的javascript在最頂端的SWFUpload代碼在瀏覽器中的實際swfuploader將擁有曲奇要求適當地固定....

+1

SWFUpload的操作您的瀏覽器範圍之外。它不會使用你的瀏覽器的cookies。 – dirkbonhomme

+0

如果您刪除並禁用所有Cookie,會發生什麼情況? – Henridv

回答

3

默認PHP會話處理機制使用您的Cookie和您的用戶代理將會話與用戶進行匹配。由於Flash通過「ShockWave Flash」的用戶代理向服務器標識了自己,這與瀏覽器的UA不同,因此php會爲請求分配一個新會話。

如果您輸出_POST數據,然後將這些值與您在session_start()之後獲得的值進行比較,或者您將瀏覽器的UA更改爲與Flash發送的完全匹配,則可以檢查該數據。

解決方案?

將會話ID傳遞給服務器,無論您希望如何,默認值爲_POST,並使用正確的ID初始化會話。

SWFUpload爲您提供post_params即「defines the name/value pairs that will be posted with each uploaded file」。

// SWFUpload example - taken from you own config file 
post_params : { 
    "sid" : sid => THIS WOULD BE YOUR SESSION ID!; you can put <?=session_id()?> 
}, 

然後在服務器端...

// Prior to starting the session as you normally do you'll want to check if 
// you're trying to pass a session id by post, if so, initialize session with it 
<?php 
if(isset($_POST['sid'])) 
{ 
    session_id($_POST["sid"]); 
} 
session_start(); 
+0

我收到只在Firefox中相同的錯誤。鍍鉻工作正常。我正在使用asp.net。那有什麼原因? @Frankie –

+0

@SpiralsWhirls我會想你的問題是完全一樣的OP和解決方案相似。您是否嘗試通過'post_params'傳遞會話ID? – Frankie

+0

很抱歉bieng遲了......其實我正在使用Asp.net。 –