我建立一個遠程URL上傳到不同的服務器與進度, 現在我有這2個領域,即域A和B從域B傳遞<script>到域A
我要上傳遠程URL文件從域A到域B,我有一個PHP功能在域B回聲< script> parentFunction(); < /腳本域A>,
我知道這是不可能的,因爲安全問題的任何瀏覽器,但我只是想詢問是否有此
一個解決辦法,這是在我的PHP代碼域B,如果有人需要
function upload(url)
{
if (function_exists('stream_context_create'))
{
$httpArr = array(
'timeout' => 15, // 15 seconds
);
$httpArr[ 'notification' ] = [ $this, 'remote_url_progress_callback' ];
$ctx = stream_context_create();
stream_context_set_params($ctx, $httpArr);
}
}
function remote_url_progress_callback($notification_code, $severity, $message, $message_code, $bytes_transferred, $bytes_max)
{
switch($notification_code) {
case STREAM_NOTIFY_RESOLVE:
case STREAM_NOTIFY_AUTH_REQUIRED:
case STREAM_NOTIFY_FAILURE:
case STREAM_NOTIFY_AUTH_RESULT:
break;
case STREAM_NOTIFY_COMPLETED:
echo '<script>parent.remoteProgress('. json_encode(array('loaded' => $bytes_max, 'total' => $bytes_max)) .');</script>';
break;
case STREAM_NOTIFY_REDIRECTED:
break;
case STREAM_NOTIFY_CONNECT:
echo '<script>parent.rstartTime = (new Date()).getTime();</script>';
break;
case STREAM_NOTIFY_FILE_SIZE_IS:
echo '<script>parent.remoteProgress('. json_encode(array('loaded' => 0, 'total' => $bytes_max)) .');</script>';
break;
case STREAM_NOTIFY_MIME_TYPE_IS:
break;
case STREAM_NOTIFY_PROGRESS:
echo '<script>parent.remoteProgress('. json_encode(array('loaded' => $bytes_transferred, 'total' => $bytes_max)) .');</script>';
break;
}
ob_flush();
flush();
}
這是我的代碼在域A
function remoteProgress(value)
{
var percentageDone = parseInt(value.loaded/value.total * 100, 10);
nowTime = (new Date()).getTime();
loadTime = (nowTime - rstartTime);
if (loadTime == 0)
{
loadTime = 1;
}
loadTimeInSec = loadTime/1000
bytesPerSec = value.loaded/loadTimeInSec;
var textContent = '';
textContent += 'Progress: '+percentageDone+'%';
textContent += ' ';
textContent += '('+bytesToSize(value.loaded, 2)+'/'+bytesToSize(value.total, 2)+')';
textContent += '<br />Speed: '+ bytesToSize(bytesPerSec, 2) +'/s. ';
progressText = textContent;
$('#remote-progress-bar').css('display', 'inline-block');
$('#remote-progress-bar .bar').css(
'width',
percentageDone + '%'
);
$('#remote-progress-text').html(progressText);
}
//域名只是虛擬
$('#remote-upload-progresser').prop('src', 'http://123.123.122.199/test/fileshare/remote/do-remote-upload/?remote[url_files]='+ encodeURIComponent(remote_files.join("@@[email protected]@@")) );
如果您對兩個域都有控制權,那麼沒有任何安全問題都是可能的。 – Bergi 2015-01-31 22:15:13
我有兩個域的控制權,我的問題是在瀏覽器本身im有這個錯誤。 錯誤權限被拒絕訪問從域B到域A的屬性 – eaponz 2015-02-01 07:05:03
使用CORS應該是最簡單的解決方案。但是,如果您需要進一步的幫助,請同時顯示域A的代碼。我仍然不確定您想要做什麼,以及您獲得了哪些錯誤。 – Bergi 2015-02-01 13:12:55