草庐IT

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

coder 2024-06-20 原文

我使用 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/

有关ios - iOS 应用程序生成的 .CSV 文件在 Windows 中不可读的更多相关文章

  1. ruby - 使用 RubyZip 生成 ZIP 文件时设置压缩级别 - 2

    我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看ruby​​zip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d

  2. ruby - 在 Ruby 程序执行时阻止 Windows 7 PC 进入休眠状态 - 2

    我需要在客户计算机上运行Ruby应用程序。通常需要几天才能完成(复制大备份文件)。问题是如果启用sleep,它会中断应用程序。否则,计算机将持续运行数周,直到我下次访问为止。有什么方法可以防止执行期间休眠并让Windows在执行后休眠吗?欢迎任何疯狂的想法;-) 最佳答案 Here建议使用SetThreadExecutionStateWinAPI函数,使应用程序能够通知系统它正在使用中,从而防止系统在应用程序运行时进入休眠状态或关闭显示。像这样的东西:require'Win32API'ES_AWAYMODE_REQUIRED=0x0

  3. ruby - 将差异补丁应用于字符串/文件 - 2

    对于具有离线功能的智能手机应用程序,我正在为Xml文件创建单向文本同步。我希望我的服务器将增量/差异(例如GNU差异补丁)发送到目标设备。这是计划:Time=0Server:hasversion_1ofXmlfile(~800kiB)Client:hasversion_1ofXmlfile(~800kiB)Time=1Server:hasversion_1andversion_2ofXmlfile(each~800kiB)computesdeltaoftheseversions(=patch)(~10kiB)sendspatchtoClient(~10kiBtransferred)Cl

  4. ruby - 如何指定 Rack 处理程序 - 2

    Rackup通过Rack的默认处理程序成功运行任何Rack应用程序。例如:classRackAppdefcall(environment)['200',{'Content-Type'=>'text/html'},["Helloworld"]]endendrunRackApp.new但是当最后一行更改为使用Rack的内置CGI处理程序时,rackup给出“NoMethodErrorat/undefinedmethod`call'fornil:NilClass”:Rack::Handler::CGI.runRackApp.newRack的其他内置处理程序也提出了同样的反对意见。例如Rack

  5. ruby - 在 Ruby 中编写命令行实用程序 - 2

    我想用ruby​​编写一个小的命令行实用程序并将其作为gem分发。我知道安装后,Guard、Sass和Thor等某些gem可以从命令行自行运行。为了让gem像二进制文件一样可用,我需要在我的gemspec中指定什么。 最佳答案 Gem::Specification.newdo|s|...s.executable='name_of_executable'...endhttp://docs.rubygems.org/read/chapter/20 关于ruby-在Ruby中编写命令行实用程序

  6. ruby - 用逗号、双引号和编码解析 csv - 2

    我正在使用ruby​​1.9解析以下带有MacRoman字符的csv文件#encoding:ISO-8859-1#csv_parse.csvName,main-dialogue"Marceu","Giveittohimóhe,hiswife."我做了以下解析。require'csv'input_string=File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")#=>"Name,main-dialogue\r\n\"Marceu\",\"Giveittohim\x97he,hiswife.\"\

  7. ruby - 我可以使用 Ruby 从 CSV 中删除列吗? - 2

    查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html

  8. ruby-on-rails - Rails 应用程序之间的通信 - 2

    我构建了两个需要相互通信和发送文件的Rails应用程序。例如,一个Rails应用程序会发送请求以查看其他应用程序数据库中的表。然后另一个应用程序将呈现该表的json并将其发回。我还希望一个应用程序将存储在其公共(public)目录中的文本文件发送到另一个应用程序的公共(public)目录。我从来没有做过这样的事情,所以我什至不知道从哪里开始。任何帮助,将不胜感激。谢谢! 最佳答案 无论Rails是什么,几乎所有Web应用程序都有您的要求,大多数现代Web应用程序都需要相互通信。但是有一个小小的理解需要你坚持下去,网站不应直接访问彼此

  9. ruby - 无法运行 Rails 2.x 应用程序 - 2

    我尝试运行2.x应用程序。我使用rvm并为此应用程序设置其他版本的ruby​​:$rvmuseree-1.8.7-head我尝试运行服务器,然后出现很多错误:$script/serverNOTE:Gem.source_indexisdeprecated,useSpecification.Itwillberemovedonorafter2011-11-01.Gem.source_indexcalledfrom/Users/serg/rails_projects_terminal/work_proj/spohelp/config/../vendor/rails/railties/lib/r

  10. ruby - 在 jRuby 中使用 'fork' 生成进程的替代方案? - 2

    在MRIRuby中我可以这样做:deftransferinternal_server=self.init_serverpid=forkdointernal_server.runend#Maketheserverprocessrunindependently.Process.detach(pid)internal_client=self.init_client#Dootherstuffwithconnectingtointernal_server...internal_client.post('somedata')ensure#KillserverProcess.kill('KILL',

随机推荐