###前提・実現したいこと
iOS 10.0以上
XCode 8.3.3
アプリ内で入力した情報を端末内にファイルとして保存しています。
保存したファイルをPCのブラウザからダウンロードさせたく、CocoaHTTPServerを使ってみましたが実現できていない状態です。
###試したこと
次のコードはMyHTTPConnection内のhttpResponseForMethodメソッドをカスタマイズしたものです。
アプリ内にsample.txtを配置して、PCのブラウザからアクセスすると、ブラウザにsample.txtの中身の羅列が表示されました。。
このカスタマイズは本家のサンプルをコピーしたもので、コメントアウトした部分がありますが、そこに置き換えても結果は同じでした。
objective
1- (NSObject<HTTPResponse> *)httpResponseForMethod:(NSString *)method URI:(NSString *)path 2{ 3 HTTPLogTrace(); 4 5 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 6 NSString *strDocPath = paths[0]; 7 8 NSString *filePath = [strDocPath stringByAppendingPathComponent:@"sample.txt"]; 9 10 BOOL isDir = NO; 11 if (filePath && [[NSFileManager defaultManager] fileExistsAtPath:filePath isDirectory:&isDir] && !isDir) 12 { 13 return [[HTTPFileResponse alloc] initWithFilePath:filePath forConnection:self]; 14 15 // Use me instead for asynchronous file IO. 16 // Generally better for larger files. 17 18 //return [[HTTPAsyncFileResponse alloc] initWithFilePath:filePath forConnection:self]; 19 } 20 21 return [super httpResponseForMethod:method URI:path]; 22}
###試したこと②
アプリ内のHTTPファイルにダウンロードタグを配置し、パスを動的に変更させてみました。
表示されるリンクの内容は、アプリ内で参照できるURIになっていますがクリックしても何も起こりませんでした。
リンクを右クリックで「対象をファイルに保存」ではファイル名だけは正しい0バイトのファイルがダウンロードされました。
html
1<!DOCTYPE HTML> 2<html lang="ja"> 3<head> 4 <meta charset="utf-8" /> 5 <title>ファイルダウンロード</title> 6</head> 7<body> 8 <form action="upload.html" method="get" enctype="multipart/form-data" accept-charset="utf-8"> 9 <a href=%RPATH%>Download</a><br/> 10 <br/> 11 </form> 12</body> 13</html> 14
objective
1- (NSObject<HTTPResponse> *)httpResponseForMethod:(NSString *)method URI:(NSString *)path 2{ 3 HTTPLogTrace(); 4 5 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 6 NSString *strDocPath = paths[0]; 7 8 NSString *csvFilePath = [strDocPath stringByAppendingPathComponent:@"sample.txt"]; 9 NSDictionary* replacementDict = [NSDictionary dictionaryWithObjectsAndKeys:csvFilePath, @"RPATH", nil]; 10 11 NSString* templatePath = [[config documentRoot] stringByAppendingPathComponent:@"download/index.html"]; 12 return [[HTTPDynamicFileResponse alloc] initWithFilePath:templatePath forConnection:self separator:@"%" replacementDictionary:replacementDict]; 13 14 15} 16
アプリ側に持たせるhtmlファイルの書き方も含めてご教授頂けると有難いです。
そもそもですが、CocoaHTTPServer以外で実現できる方法があれば乗り換えることは大丈夫です。
ただしPCやiOS端末がインターネットに接続しない想定のため、DropboxやGoogleDriveは使えない状態です。
よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。