Jump to content

php imap: save attachment to desktop via jQuery ajax..


antonyfal
Go to solution Solved by antonyfal,

Recommended Posts

Hi..

I trying to select an attachment from a dropdown select and then i want it to save to the desktop. Everything works perfectly the files all save, however if a file has a name with spaces, the file that saves only take the first word of the name, and the extension is not saved.

 

IE: $filename = amy numbers.txt

 

the file name of the downloaded file is amy and saves without an extension.

 

here is my code: both php and jquery javascript:

 

 
// here is javascript call
<script>
jQuery('ul.ligtatchment > li >a.att_dwn').live('click', function (){
jQuery('#loadingdiv').show();
var msg = jQuery(this).attr('rel').split('_');
    jQuery.get(globalVar +'&task=em_attachment&msgnum='+msg[0]+'&part='+msg[1]+'&encoding='+msg[2], function(data) {
    if(data){
    jQuery('#atdiv_'+msg[0]).remove();
jQuery('#loadingdiv').hide();
    jQuery('<div class="attdiv" id="atdiv_'+msg[0]+'"><iframe id="dnframe_'+msg[0]+'" name="dnframe_'+msg[0]+'" class="dnframe" src="'+globalVar +'&task=em_attachment&msgnum='+msg[0]+'&part='+msg[1]+'&encoding='+msg[2]+'" style="display: none;"></iframe></div>').prependTo('div#chkmldivs');    
    } else {
    jQuery('#loadingdiv').hide();
    jAlert('ERROR', 'There is no file for download');
    }
    });
    
});
</script>
 
// here is php code..
$msgnum = $_GET['msgnum'];
$partNum =$_GET['part'];
$encoding = $_GET['encoding'];

    $partStruct = imap_bodystruct($imap, $msgnum, $partNum);
    $filename = $partStruct->dparameters[0]->value;  // if i echo the name here it is correct IE: amy numbers.txt
    //$nfilename = str_replace(' ',' ',$filename); here i tried to replace the blanks with   but did not work..
    $message = imap_fetchbody($imap, $msgnum, $partNum);
    switch ($encoding) {
        case 0:
        case 1:
            $message = imap_8bit($message);
            break;
        case 2:
            $message = imap_binary($message);
            break;
        case 3:
            $message = imap_base64($message);
            break;
        case 4:
            $message = quoted_printable_decode($message);
            break;
    }
 
    header("Content-Description: File Transfer");
    header("Content-Type: application/octet-stream");
    header("Content-Disposition: attachment; filename=".$filename);
    header("Content-Transfer-Encoding: binary");
    header("Expires: 0");
    header("Cache-Control: must-revalidate");
    header("Pragma: public");
    echo $message;
 

Link to comment
Share on other sites

Thanks jairathnem..

if i try to put the $filename in quotes and also encode the $filename..

it does not download, only an image downloads, the text files open for display..

heres what i have tried since posting:

 

    header("Content-Description: File Transfer");
    header("Content-Type: application/octet-stream");
    
    if(strpos( $_SERVER['HTTP_USER_AGENT'],"MSIE" )>0)
    {
    header('Content-Disposition: attachment; filename=" '.urlencode($fileName).' " ');
    }
    else
    {
    header('Content-Disposition: attachment; filename*=UTF-8\' \' '.urlencode($fileName));// there is something wrong with this part..
    }
    header('Content-Transfer-Encoding: binary');
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    echo $message;

Edited by antonyfal
Link to comment
Share on other sites

Hi.. I tried many things to download the files with a .txt extension and spaces in the name, i did manage to get the fix for the spaces in the name of a .txt file name by adding colons.. However now when i try to download files with an .txt file extension, it simply opens the file and does not download it,, regardless whether the name is one word or multiple words...

 

What modification would i have to make to the following code to download .txt files:

here is my newly adjusted attempt at downloading the files from server:

// this is just the php that i changed
 
    $message = base64_decode(imap_fetchbody($imap, $msgnum, $partNum));
    $ext = substr($filename, strrpos($filename, '.') + 1);
    
    $next = strtolower($ext);
     $type = "application/octet-stream";
    if($next =='asf'){
        $type = "video/x-ms-asf";
        }
    if($next =='avi'){
        $type = "video/avi";
        }
    if($next =='flv'){
        $type = "video/x-flv";
        }
    if($next =='fla'){
        $type = "application/octet-stream";
        }
    if($next =='swf'){
        $type = "application/x-shockwave-flash";
        }        
    if($next =='doc'){
        $type = "application/msword";
        }
    if($next =='docx'){
        $type = "application/msword";
        }
    if($next =='zip'){
        $type = "application/zip";
        }
    if($next =='xls'){
        $type = "application/vnd.ms-excel";
        }
    if($next =='gif'){
        $type = "image/gif";
        }
    if($next =='jpg'){
        $type = "image/jpg";
        }
    if($next =='jpeg'){
        $type = "image/jpg";
        }
    if($next =='png'){
        $type = "image/png";
        }        
    if($next =='wav'){
        $type = "audio/wav";
        }
    if($next =='mp3'){
        $type = "audio/mpeg3";
        }
    if($next =='mpg'){
        $type = "video/mpeg";
        }
    if($next =='mpeg'){
        $type = "video/mpeg";
        }
    if($next =='rtf'){
        $type = "application/rtf";
        }
    if($next =='html'){
        $type = "text/html";
        }
    if($next =='htm'){
        $type = "text/html";
        }
    if($next =='xml'){
        $type = "text/xml";
        }    
    if($next =='xsl'){
        $type = "text/xsl";
        }
    if($next =='css'){
        $type = "text/css";
        }
    if($next =='php'){
        $type = "text/php";
        }
    if($next =='txt'){
        $type = "text/plain";
        }
    if($next =='asp'){
        $type = "text/asp";
        }
    if($next =='pdf'){
        $type = "application/pdf";
        }
    if($next =='psd'){
        $type = "application/octet-stream";
        }
        
                
    header("Content-Description: File Transfer");
    header("Content-Type: $type; name=\"$filename\"");
    header("Content-Disposition: attachment; filename=\"$filename\"");
    header("Content-Transfer-Encoding: binary");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Pragma: public");
    header("Content-Length: ".filesize($message));
    ob_clean();
    flush();
    echo $message;

Link to comment
Share on other sites

for force download you should set Content-Type: application/octet-stream

looks like you change it if its txt, hence it is displaying and not downloading.

I have tried everything even force download and and and ... ive just finished downloading the file to my server and downloading it from there but nothing is working.. I have discovered that i do have the filename fixed though...

 

how can i download a txt file from the server?

 

all my headers are correct.. even if i add the url to the browser the file is displayed on the page and the browser download dialog does not appear..

 

any ideas?

i would like to download the file via jquery ajax..

Link to comment
Share on other sites

  • Solution

PRogress:

 

It was always downloading the attachments. This morning i opened my download folder and viewed about 100 files that were downloaded through my testing. The download dialog never appeared and the downloads were so small it barely showed up on mozilla..

 

Anyway this is solved.. All the attempts above work.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.