0

Actually i am downloading the file from my ios app using ASIHTTPRequest Library. I want to pass the international characters(it's a file name) via php header response while downloading the file. Basically am creating the new custom header called "filename-display" while construction the file download header response.

Here my PHP code:

<?php

    $dir = 'c:\좋은 아침.pdf';
    $filename_display = '좋은 아침.pdf';

    if (file_exists($dir)) {
                header("HTTP/1.1 200"); 
                header('Content-Description: File Transfer');
                header('Content-Type: application/octet-stream');
                header('Content-Disposition: attachment; filename='.basename($dir));
                header('Content-Transfer-Encoding: binary');
                header('Expires: 0');
                header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
                header('Pragma: public');
                header('filename-display:' . $filename_display);
                header('Content-Length:' . filesize($dir));

                ob_clean();
                flush();
                readfile($dir);

                exit;
    } 
    else {          
            header("HTTP/1.1 404 file not found");      
            echo 'file_not_exists';
            exit(0);
    }
?>

Client Code ASIHTTPRequest Library Delegate calls:

#pragma mark
#pragma ASIHTTPREQUEST delegate methods
-(void)requestStarted:(ASIHTTPRequest *)request{

    NSLog(@"\n file download start");
}


-(void)updateUploadProgress:(ASIHTTPRequest*)request
{
    [request updateUploadProgress];
}


- (void)requestFailed:(ASIHTTPRequest *)request {
//handle it
}

- (void)requestFinished:(ASIHTTPRequest *)request {
    NSDictionary *responseHeaders = [request responseHeaders];

    NSLog(@"\nresponse header dict = %@", responseHeaders);

    NSLog(@"\nfile name: %@", [responseHeaders objectForKey:@"filename-display"]);
}

Output on my ios app:

file name: ?????????.pdf instead of shows the actual file name configured on php header side.

Let me know what encoding i can use on php side?. Also it's possible to pass the xml string via header response?.

Any help that might me appreciated.

-loganathan

loganathan
  • 2,056
  • 2
  • 23
  • 34
  • Btw., _custom_ headers should always start with `X-`, so e.g. `X-My-Custom-Header`, to avoid any possible interference with actually defined headers (or headers that might get defined in the future). – CBroe Mar 26 '13 at 12:57

3 Answers3

2

Using a custom header means that only custom clients will look for it. I'm not sure if it's the right choice, however a better alternative is both using your custom header and the standard one Content-Disposition.

Based on this answer, there are multiple strategies:

  • RFC 5987 encoding
  • Direct UTF-8 encoding
  • IE (UTF-8, percent sign) encoding

You can switch on the user agent to build your HTTP response. Another option is using a UTF-8 encoded URL for the download link (so the browser will suggest the last portion of the path, ie the filename).

It's hard to tell why your iOS app fails, because it depends on the HTTP library used and how you decode the bytes in the header, and without the code one can't tell.

Community
  • 1
  • 1
Raffaele
  • 20,627
  • 6
  • 47
  • 86
  • Raffaele I am using ASIHTTTPRequest on ios side. This is a standard library. I hope this will handle all the encoded format as a default. I just printed the header response dictionary on console and then it prints "?????.pdf". Any how i updated my client code for your reference please check it once. So, here i just want to know the problem is constructing the header on server side or i need to do something with ASIHTTPRequest library on client side. – loganathan Mar 27 '13 at 05:43
  • The *problem* is agreeing on a protocol. Again, I suggest **not** using a custom header for this task, but instead look at [RFC 5987](http://greenbytes.de/tech/webdav/rfc5987.html#character.set.and.language.information) and [RFC 2231](http://greenbytes.de/tech/webdav/rfc2231.html). Then, post a hex dump of the string given as the value for the `filename` parameter in the `Content-Disposition` header (it will look like `Content-Disposition: attachment; filename*=UTF-8''foo-%c3%a4-%e2%82%ac.html`) – Raffaele Mar 27 '13 at 10:42
0

I had a similar problem in a web page, and I solved it putting the proper encoding declaration in the HTTP header field Content-Type.

Content-Type: text/html;charset=utf-8

maybe if you do the same...

Natalia
  • 470
  • 2
  • 5
  • 12
  • This tells how to interpret the bytes in the response body, while he's having troubles with suggesting a name via a header – Raffaele Mar 26 '13 at 13:04
0

Finally i figured it out how to do that. Here my solution 1. In PHP side i used json decode. Since i decoded the file name either ascii or non-ascii and then i set that in header. 2. On iOS side i used following code to convert the encoded json to nsstring.

NSString *f_Name = <your jason string>;
NSString *convertedString = [f_Name mutableCopy];

 //convert json encoded string to nsstring
 CFStringRef transform = CFSTR("Any-Hex/Java");
 //CFStringTransform((__bridge CFMutableStringRef)convertedString, NULL, transform, YES);
 CFStringTransform((CFMutableStringRef)convertedString, NULL, transform, YES);

 f_Name = convertedString;

 //remove the double quotes from first and last position
 f_Name = [f_Name substringFromIndex:1];
 f_Name = [f_Name substringToIndex:[f_Name length] - 1];
loganathan
  • 2,056
  • 2
  • 23
  • 34