从服务器一个一个下载文件

     2023-02-24     230

关键词:

【中文标题】从服务器一个一个下载文件【英文标题】:Download file from server one by one 【发布时间】:2017-02-13 06:20:42 【问题描述】:

我必须在 UITableViewCell 中单击相应按钮时从服务器下载文件。当用户点击按钮以在一个单元格中下载时,应该开始下载。下载完成后,我将保存核心数据。到目前为止一切顺利。但是在下载当前文件时,如果用户点击以在 Cell 中下载另一个文件,它也应该下载然后保存到核心数据并可供播放。而且我在每个表格单元格中有不同的 url。如果用户点击多个按钮应该下载它们并保存到核心数据。这是我的代码。

            NSString *url=[[chatHistoryArr objectAtIndex:sender.tag]valueForKey:@"voice"];
        NSLog(@"%@",url);
        //NSURL *voiceUrl=[NSURL URLWithString:url];

        tempDict=[[NSMutableDictionary alloc]init];

        [tempDict setValue:[[chatHistoryArr objectAtIndex:sender.tag]valueForKey:@"msg_id"] forKey:@"msg_id"];
        [tempDict setValue:[[chatHistoryArr objectAtIndex:sender.tag]valueForKey:@"to"] forKey:@"to"];
        [tempDict setValue:[[chatHistoryArr objectAtIndex:sender.tag]valueForKey:@"from"] forKey:@"from"];
        [tempDict setValue:[[chatHistoryArr objectAtIndex:sender.tag]valueForKey:@"time"] forKey:@"time"];

        UIImageView* animatedImageView = [[UIImageView alloc] initWithFrame:cell.playButton.bounds];
        animatedImageView.animationImages = [NSArray arrayWithObjects:
                                             [UIImage imageNamed:@"1.png"],
                                             [UIImage imageNamed:@"2.png"],
                                             [UIImage imageNamed:@"3.png"],
                                             [UIImage imageNamed:@"4.png"], nil];
        animatedImageView.animationDuration = 3.0f;
        animatedImageView.animationRepeatCount = 10;
        [animatedImageView startAnimating];
        [cell1.playButton addSubview: animatedImageView];

        NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
        AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:configuration];
        [manager setResponseSerializer:[AFHTTPResponseSerializer serializer]];
        // manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"application/octet-stream",@"video/3gpp",@"audio/mp4",nil];
        NSURL *URL = [NSURL URLWithString:url];
        NSURLRequest *request = [NSURLRequest requestWithURL:URL];

        NSURLSessionDataTask *dataTask = [manager dataTaskWithRequest:request completionHandler:^(NSURLResponse *response, id responseObject, NSError *error) 
            if (error)
            
                NSLog(@"Error: %@", error);
             else
            

                NSData *data=[[NSData alloc]initWithData:responseObject];

            //Here I'm saving file to local storage then updating UI.
                    [self sentMsgSaveWithData:data orUrl:@"" withBool:YES withMsg_ID:@"" withDict:tempDict];

            
        ];
        [dataTask resume];

在这里,我设法一次只下载一个文件,完成后,如果用户点击另一个单元格,那么只有我正在下载它。但是我必须在单元格中的多个按钮点击上下载多个文件。 我一直在努力实现这一点。请提出一些建议。 提前致谢。

【问题讨论】:

How to download files and save to local using AFNetworking 3.0?的可能重复 您会将 url 传递给一种方法,比如说 openurl(NSUrl*) mypicUrl :(Int) cellrow。所以你知道在下载结束时必须重新加载哪个单元格。 【参考方案1】:

在 MVC 模式中,单元格是一个视图,不应处理数据解析和下载内容。最好在您的模型中执行此操作。但为简单起见,它通常放在控制器中。

    将模型数组保存在控制器中并将数据传递到单元格 将单元格的按钮操作配置到控制器(委托或阻止或通知...) 将下载代码放入控制器并更新模型状态并在完成后重新加载 tableView。

细胞.h

#import <UIKit/UIKit.h>
#import "YourCellProtocol.h"

typedef NS_ENUM(NSInteger, YourCellStatus) 
    YourCellStatusNormal,
    YourCellStatusDownloading,
    YourCellStatusCompleted
;

@interface YourCell : UITableViewCell

@property (nonatomic, weak) id<YourCellProtocol> delegate;

@property (nonatomic, assign) YourCellStatus status;

@property (nonatomic, weak) id yourDataUsedToShownInUI;

@end

细胞.m

#import "YourCell.h"

@interface YourCell ()

@property (nonatomic, strong) UIButton *myButton;

@end

@implementation YourCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier 
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) 
        // init all your buttons etc
        _myButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [_myButton addTarget:self action:@selector(myButtonPressed) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:_myButton];
    
    return self;


- (void)setStatus:(YourCellStatus)status 
    //update your cell UI here


- (void)myButtonPressed 
    // tell your controller to start downloading
    if (self.status != YourCellStatusNormal) 
        [self.delegate didPressedButtonInYourCell:self];
    


@end

控制器.m

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
    static NSString *CellID = @"YourCellID";
    YourCell *cell = (YourCell *)[tableView dequeueReusableCellWithIdentifier:CellID];

    if (cell == nil) 
        cell = [[YourCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellID];
    

    cell.delegate = self;

    YourModel *model = self.yourDataArray[indexPath.row];
    cell.yourDataUsedToShownInUI = model.dataToShownInUI;
    if (model.downloading) 
        cell.status = YourCellStatusDownloading;
     else if (model.completed) 
        cell.status = YourCellStatusCompleted;
     else 
        cell.status = YourCellStatusNormal;
    

    //other configs ...

    return cell;


- (void)didPressedButtonInYourCell:(id)sender 
    NSIndexPath *indexPath = [self.tableView indexPathForCell:sender];
    YourModel *model = self.yourDataArray[indexPath.row];
    model.downloading = YES;

    //start downloading
    //...
    // in download completion handler, update your model status, and call
    //[self.tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];

【讨论】:

从服务器下载最新文件版本

】从服务器下载最新文件版本【英文标题】:Downloadinglatestfileversionfromserver【发布时间】:2014-03-2203:45:32【问题描述】:您好,我有一个应用程序正在尝试添加内置更新程序。该应用程序包含一个从我的服务器下载apk文件的按钮... 查看详情

从服务器下载文件时进度条卡在 0%?

】从服务器下载文件时进度条卡在0%?【英文标题】:ProgressBargetStuckedat0%whiledownloadingfilefromserver?【发布时间】:2016-12-2422:23:53【问题描述】:我正在创建一个Android应用程序来下载一个mp3文件服务器。下载工作正常。但是ProgressBa... 查看详情

从nodejs服务器下载文件在客户端下载为损坏的文件

】从nodejs服务器下载文件在客户端下载为损坏的文件【英文标题】:Downloadingfilesfromnodejsserverdownloadascorruptedfilesontheclientside【发布时间】:2020-11-1003:00:09【问题描述】:在客户端,它是一个反应应用程序。我设置了一个端点,我... 查看详情

从 archive.org 下载文件

...它给出了一个0KB的文件,使用相同的脚本,并从我自己的服务器下载相同的文件,它变成了TRUE,并且文件已下载。这是脚本,提示链接:$saveit=\'<ahref="Files/direct_download 查看详情

java从服务器下载文件并保存到本地

昨天在做一个项目时,用到了从服务器上下载文件并保存到本地的知识,以前也没有接触过,昨天搞了一天,这个小功能实现了,下面就简单的说一下实现过程;  1.基础知识     当我们想要下载网站上... 查看详情

.exe 文件从服务器下载时损坏

】.exe文件从服务器下载时损坏【英文标题】:.exeFilebecomescorruptedwhendownloadedfromserver【发布时间】:2010-03-1621:06:01【问题描述】:首先:我是一个卑微的网页设计师,他知道的PHP是危险的,而且对服务器管理的了解也足够了,好... 查看详情

PHP:从服务器下载时保持相同的文件名

】PHP:从服务器下载时保持相同的文件名【英文标题】:Php:Keepsamefilenamewhendownloadingfromserver【发布时间】:2013-08-2020:05:34【问题描述】:我正在通过php脚本下载一个文件,除了一个丑陋的事实之外,一切都运行良好。下载的文件... 查看详情

jQuery从一个按钮下载一个zip文件?

】jQuery从一个按钮下载一个zip文件?【英文标题】:jQuerydownloadazipfilefromabutton?【发布时间】:2010-05-2023:23:41【问题描述】:很尴尬我花了多少时间试图从一个按钮下载一个zip文件....<buttontype=\'button\'id=\'button-download\'>downloadzi... 查看详情

java实现从服务端下载文件

这边用一个简单的servlet实现java从服务端下载文件的操作  写一个servlet:<servlet><servlet-name>DownloadServlet</servlet-name><servlet-class>DownloadServlet</servlet-class></servlet> 查看详情

从href链接下载文件,如何? [关闭]

...述】:我有一个带有href的标签。单击链接时,想从我的服务器下载文件。我怎样才能做到这一点。这是我的标签"<ahref=\\""+downloadPDF("xxx","xxx")+"\\">downloadPDF是一个开始下载的方法,但是如 查看详情

拒绝通过 URL 从网络服务器直接下载文件

】拒绝通过URL从网络服务器直接下载文件【英文标题】:DenydirectfiledownloadfromwebserverbyURL【发布时间】:2021-11-1614:54:32【问题描述】:我有一个网站,用户可以登录并由sessions和$user[id]标识。他们可以上传文件。文件存储在htdocs/up... 查看详情

如何让 ASP.NET 页面知道文件何时可以从服务器下载

】如何让ASP.NET页面知道文件何时可以从服务器下载【英文标题】:HowtoletaASP.NETpageknowwhenafileisreadyfordownloadfromtheserver【发布时间】:2020-03-1520:28:09【问题描述】:我正在维护一个旧版VB.NetWebforms应用程序,添加一个部分后我遇到... 查看详情

如何将文件从 URL 下载到服务器文件夹

】如何将文件从URL下载到服务器文件夹【英文标题】:HowtodownloadafilefromanURLtoaserverfolder【发布时间】:2021-10-1202:04:47【问题描述】:我正在开发一个ASP.NETCoreWeb应用程序并且我正在使用RazorPages。我的应用程序中显示了一些URL,当... 查看详情

处理从 Amazon S3 下载的多个文件?

...尺寸图像的私人存储桶,所有缩略图和较小尺寸都在网络服务器上。当用户想要下载多张图片时,我想压缩上述图片,然后将它们作为一个文件交付给用户。目前,我能想到的唯一方法是将文件从S3传输到Web服务器,压缩,然后... 查看详情

从 FTP 服务器检查和下载多个文件

】从FTP服务器检查和下载多个文件【英文标题】:CheckandDownloadMultiplefilesfromFTPserver【发布时间】:2012-04-2804:41:25【问题描述】:我使用从FTP服务器上传和下载文件的概念。我在一个文件中都成功了。现在,为了下载,ftp上可能有... 查看详情

从服务器下载文件一直失败,但相同的 obj-c 代码在另一个环境和 IOS 应用程序中工作

】从服务器下载文件一直失败,但相同的obj-c代码在另一个环境和IOS应用程序中工作【英文标题】:Downloadingafilefromserverkeepsfailing,butthesameobj-ccodeworksinanotherenvironmentandIOSapp【发布时间】:2015-10-1509:21:41【问题描述】:IOS版本是9,X... 查看详情

从服务器下载文件的最佳方法是啥

】从服务器下载文件的最佳方法是啥【英文标题】:Whatisthebestwaytodownloadfilefromserver从服务器下载文件的最佳方法是什么【发布时间】:2012-06-1008:20:44【问题描述】:我有一个有趣的任务,需要我从服务器(ASP.NET)将动态生成的文... 查看详情

用python从网上下载一个excel文件

】用python从网上下载一个excel文件【英文标题】:downloadinganexcelfilefromthewebinpython【发布时间】:2014-10-1410:36:13【问题描述】:我有以下网址:dls="http://www.muellerindustries.com/uploads/pdf/UWSPD0114.xls"我尝试下载文件:urllib2.urlopen(dls,"test.... 查看详情