我是新来的AWS,我做了一些文件上传到AWS S3与TransferUtility文件转换。在这里,我的场景步骤
1.从iCloud中选择文件
public func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentAt url: URL) {
let fileurl: URL = url as URL
let filename = url.lastPathComponent
let file-extension = url.pathExtension
let filedata = url.dataRepresentation
// Call upload function
upload(file: fileurl, keyname: filename, exten: file-extension)
// Append names into array
items.append(item(title: filename, size: string))
self.tableView_util.reloadData()2.使用传输实用程序将该文件上载到AWS S3中
private func upload(file url: URL, keyname : String, exten: String) {
transferUtility.uploadfile(file ur,
bucket: "YourBucket",
key: "YourFileName",
contentType: "text/plain",
expression: expression,
completionHandler: completionHandler).continueWith {
(task) -> AnyObject! in
if let error = task.error {
print("Error: \(error.localizedDescription)")
}
if let _ = task.result {
// Do something with uploadTask.
}
return nil;
}3.当上传需要显示每个文件上载状态到表格视图单元格时
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cellutil", for: indexPath) as! UtilityTableViewCell
let item = items[indexPath.row]
}我的问题:我可以显示的表视图上传项目,但第一次上传停止时,我上传下一个。我需要实现并行上传多个文件,并显示在细胞状态。
发布于 2018-08-14 12:57:21
要做到这一点,您需要创建一个operations,并且每个上传文件都在操作中写入网络请求,并将这些操作添加到队列中。
我在这里暗示要这样做。
创建一个具有以下属性的模型类
struct UploadRecordData {
let fileName:String
let unique_id:String
let progress:double
//...etc
}然后像这样的子类操作
struct UploadRecordOperation:Operation{
let uploadRecordData:UploadRecordData
//etc..
//update progess inside of operation class
func updateProgress(progress:Double){
uploadRecordData.progress = progress
//edited answer
let myDict = [ "progress": progress, "unique_id":unique_id]
NSNotificationCenter.defaultCenter().postNotificationName("refreshProgressBar", object:myDict);
}
}下面是表视图控制器的一部分
func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(textCellIdentifier, forIndexPath: indexPath)
let row = indexPath.row
let uploadRecordData = uploadfilesRecords[row]
//edited answer
cell.progressView.uniqud_id = uploadRecord.unique_id
cell.progressView.progress = uploadRecord.progress
return cell
}以下是更新刷新上载文件进度时刷新单元格的方法。
进度视图的子类,如下面的
struct ProgressView:YourProgressView{
var unique_id:int
//Now add notification observer to your progress view
NotificationCenter.default.addObserver(self, selector: #selector(refreshProgressView), name: "refreshProgressBar", object: nil)
func refreshProgressView(notification: NSNotification){
let dict = notification.object as! NSDictionary
let progress = dict["progress"]
let u_id = dict["unique_id"]
if u_id == self.unique_id {
self.progress = progress
}
}请参阅以上操作子类和表视图委托方法中更新的代码。
https://stackoverflow.com/questions/51839307
复制相似问题