我使用Obj C在我的应用程序中创建了一个.csv文件,并将其附加到发送的电子邮件中。所有这一切都很好,当使用OSX打开文件附件时,文件名会按照我的预期读取; " example.csv&#34 ;.
但是,当我在Windows中尝试此操作时,文件扩展名不再可见,并且"文件不可读"。当我更改文件的名称并在其末尾添加.csv时,它变得可读。
将附件下载到Windows计算机后,为什么文件扩展会丢失?
这是我定义" FilePath&#34 ;;
的地方NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *dataPath = [documentsDirectory stringByAppendingPathComponent:@"/Jobs"];
if (![[NSFileManager defaultManager] fileExistsAtPath:dataPath]){
[[NSFileManager defaultManager] createDirectoryAtPath:dataPath withIntermediateDirectories:NO attributes:nil error:&error];
}
FilePath = [documentsDirectory stringByAppendingPathComponent:[NSString stringWithFormat:@"/Jobs/%@.csv",proposalNumber]];
以下是用于生成.csv文件的代码;
// Creates a temporary GPS object that we will use to save our database as a .CSV file.
GPS *saveGPS = [[GPS alloc] init];
@synchronized(FilePath) {
[[NSFileManager defaultManager] createFileAtPath:FilePath contents:nil attributes:nil];
// Creates a file handler which will allow us to write to our file.
NSFileHandle *myHandle = [NSFileHandle fileHandleForWritingAtPath:FilePath];
// Creates and writes the first line to our CSV file, which tells the program reading it what the column titles are.
NSString *csvTitleString =@"Source/Receiver, Latitude, Longitude";
[myHandle writeData:[csvTitleString dataUsingEncoding:NSUTF8StringEncoding]];
// Creates initializes another string object which will hold each line we want to write.
NSString *csvString = [[NSString alloc] init];
// Declares an array and fills it with all GPS objects found in our Database.
NSArray *allGPS = [[NSArray alloc]initWithArray:[database getAllbyProposal:proposalNumber]];
// While the current index value is less than the length of the array write the GPS values into our file then take a new line.
for(int i=0;i <= allGPS.count;i++){
if(i < allGPS.count){
saveGPS = [allGPS objectAtIndex:i];
csvString = [NSString stringWithFormat:@"\n %@ %d, %@, %@", [saveGPS sourceReceiver], [[saveGPS positionNo] intValue], [saveGPS latitude], [saveGPS longitude]];
[myHandle seekToEndOfFile];
[myHandle writeData:[csvString dataUsingEncoding:NSUTF8StringEncoding]];
}
else if (i == allGPS.count){
@synchronized(FilePath) {
// Checks if the device can send email.
if([MFMailComposeViewController canSendMail]){
// Sets the subject to data from (our current proposal number).
[mail setSubject:[NSString stringWithFormat:@"Data from %@", proposalNumber]];
[mail setMessageBody:@"Please see the attached .CSV file." isHTML:NO];
// Finds the .CSV file we just saved, and attaches it to the email.
NSData *myattachment = [NSData dataWithContentsOfFile:[NSString stringWithFormat:@"%@", FilePath]];
[mail addAttachmentData:myattachment mimeType:@"text/csv" fileName:[NSString stringWithFormat:@"%@",proposalNumber]];
// Opens up the email screen.
[self presentViewController:mail animated:YES completion:NULL];
}
else
{
// Creates a popup window to inform the user that their email wasn't able to send.
UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"Error"
message:@"Unable to send email. Have you set up a mail account on this device?"
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* dismissAction = [UIAlertAction actionWithTitle:@"Dismiss" style:UIAlertActionStyleCancel handler:^(UIAlertAction * action) {}];
alert.view.tintColor = [UIColor orangeColor];
[alert addAction:dismissAction];
[self presentViewController:alert animated:YES completion:nil];
}
}
}
}
}
答案 0 :(得分:2)
在这一行:
[mail addAttachmentData:myattachment mimeType:@"text/csv" fileName:[NSString stringWithFormat:@"%@",proposalNumber]];
您正在设置mime类型,但文件名不包含扩展名。 Mac OS X非常智能,可以通过查看mime类型和可能的文件内容来确定要执行的操作。 Windows不是。 Windows更依赖于实际的文件扩展名。
将文件扩展名添加到文件名中,因为它附加到电子邮件中。
答案 1 :(得分:0)
您正在使用扩展程序“.xls”,而不是csv。使用“.csv”作为扩展程序。