gpt4 book ai didi

ios - iOS 应用程序生成的 .CSV 文件在 Windows 中不可读

转载 作者:可可西里 更新时间:2023-11-01 11:48:48 28 4
gpt4 key购买 nike

我使用 Obj C 在我的应用程序中创建了一个 .csv 文件,并将其附加到发送的电子邮件中。所有这些工作正常,当使用 OSX 打开文件附件时,文件名如我所料; “例子.csv”。

但是,当我在 Windows 中尝试此操作时,文件扩展名不再可见并且“文件不可读”。当我更改文件名并在其末尾添加 .csv 时,它变得可读了。

为什么附件下载到windows电脑后扩展名丢失了?

这里是我定义“FilePath”的地方;

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];
}
}
}
}
}

最佳答案

在这一行中:

[mail addAttachmentData:myattachment mimeType:@"text/csv" fileName:[NSString stringWithFormat:@"%@",proposalNumber]];

您正在设置 MIME 类型,但文件名不包含扩展名。 Mac OS X 足够智能,可以通过查看 mime 类型和潜在的文件内容来确定要做什么。 window 不是。 Windows 更依赖于实际的文件扩展名。

将文件扩展名添加到附加到电子邮件的文件名中。

关于ios - iOS 应用程序生成的 .CSV 文件在 Windows 中不可读,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36152056/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com