Commit 5571143a authored by Steven杜宇's avatar Steven杜宇

// 文件上传

parent ce9c4382
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
import UIKit import UIKit
import JXSegmentedView import JXSegmentedView
import UniformTypeIdentifiers
class YHCertificateResourceUploadVC: YHBaseViewController { class YHCertificateResourceUploadVC: YHBaseViewController {
...@@ -32,6 +33,30 @@ class YHCertificateResourceUploadVC: YHBaseViewController { ...@@ -32,6 +33,30 @@ class YHCertificateResourceUploadVC: YHBaseViewController {
return tableView return tableView
}() }()
lazy var documentPickerVC: UIDocumentPickerViewController = {
let docTypes = [ "public.image",
"com.adobe.pdf",
"com.microsoft.word.doc",
"com.microsoft.powerpoint.ppt"]
if #available(iOS 14.0, *) {
var arr:[UTType] = []
for docStr in docTypes {
if let type = UTType(docStr) {
arr.append(type)
}
}
let docPickerVC = UIDocumentPickerViewController(forOpeningContentTypes: arr)
docPickerVC.delegate = self
return docPickerVC
}
let docPickerVC = UIDocumentPickerViewController(documentTypes: docTypes, in: .open)
docPickerVC.delegate = self
return docPickerVC
}()
override func viewDidLoad() { override func viewDidLoad() {
super.viewDidLoad() super.viewDidLoad()
self.view.backgroundColor = .white self.view.backgroundColor = .white
...@@ -71,6 +96,12 @@ extension YHCertificateResourceUploadVC: UITableViewDelegate, UITableViewDataSou ...@@ -71,6 +96,12 @@ extension YHCertificateResourceUploadVC: UITableViewDelegate, UITableViewDataSou
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
if true {
selectUploadFileFromICouldDrive()
return
}
YHCertificateFilterView.sheetView.show() YHCertificateFilterView.sheetView.show()
return return
...@@ -104,3 +135,51 @@ extension YHCertificateResourceUploadVC: JXSegmentedListContainerViewListDelegat ...@@ -104,3 +135,51 @@ extension YHCertificateResourceUploadVC: JXSegmentedListContainerViewListDelegat
return view return view
} }
} }
extension YHCertificateResourceUploadVC: UIDocumentPickerDelegate {
private func selectUploadFileFromICouldDrive() {
present(documentPickerVC, animated: true)
}
func documentPicker(_ controller: UIDocumentPickerViewController, didPickDocumentsAt urls: [URL]) {
guard let url = urls.first, let fileName = url.lastPathComponent.removingPercentEncoding else {
return
}
print("\(url)")
// 读取文件数据
// 获取授权
let fileUrlAuthozied = url.startAccessingSecurityScopedResource()
if fileUrlAuthozied {
// 通过文件协调工具来得到新的文件地址,以此得到文件保护功能
let fileCoordinator = NSFileCoordinator()
var error: NSError?
fileCoordinator.coordinate(readingItemAt: url, options:[.forUploading], error: &error) {
newFileUrl in
if let fileData = try? Data(contentsOf: newFileUrl) {
// 将文件数据上传到服务器
uploadFile(fileData)
}
}
url.stopAccessingSecurityScopedResource()
if let error = error {
}
} else {
print("授权失败")
}
}
func documentPickerWasCancelled(_ controller: UIDocumentPickerViewController) {
}
func uploadFile(_ data: Data) {
let format = String(format: "%.2fM", Double(data.count)/(1024.0 * 1024.0))
print("\(format)")
}
}
...@@ -15,7 +15,7 @@ class YHUploadCertificateInfo: SmartCodable { ...@@ -15,7 +15,7 @@ class YHUploadCertificateInfo: SmartCodable {
enum CodingKeys: String, CodingKey { enum CodingKeys: String, CodingKey {
case categoryData = "category_data" case categoryData = "category_data"
case supplementList = "YHSupplementInfo" case supplementList = "supplement_list"
} }
required init() { required init() {
......
...@@ -160,7 +160,7 @@ extension YHMyViewController : UITableViewDelegate, UITableViewDataSource { ...@@ -160,7 +160,7 @@ extension YHMyViewController : UITableViewDelegate, UITableViewDataSource {
if (indexPath.row >= items.count) { return } if (indexPath.row >= items.count) { return }
// if true { if true {
// YHCertificateUploadFailTipsView.tipsView().show() // YHCertificateUploadFailTipsView.tipsView().show()
...@@ -168,10 +168,11 @@ extension YHMyViewController : UITableViewDelegate, UITableViewDataSource { ...@@ -168,10 +168,11 @@ extension YHMyViewController : UITableViewDelegate, UITableViewDataSource {
// let vc = YHUploadContentVC() // let vc = YHUploadContentVC()
// self.navigationController?.pushViewController(vc) // self.navigationController?.pushViewController(vc)
// let vc = YHCertificateResourceUploadVC()
// self.navigationController?.pushViewController(vc) let vc = YHMaterialListViewController()
// return self.navigationController?.pushViewController(vc)
// } return
}
......
...@@ -15,5 +15,73 @@ ...@@ -15,5 +15,73 @@
<string>cameraDesciption</string> <string>cameraDesciption</string>
<key>NSPhotoLibraryUsageDescription</key> <key>NSPhotoLibraryUsageDescription</key>
<string>photoLibraryDesciption</string> <string>photoLibraryDesciption</string>
<key>NSFileProviderDomainUsageDescription</key>
<string>需要访问您的文件以选择和上传</string>
<key>UIFileSharingEnabled</key>
<true/>
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeName</key>
<string>PDF Document</string>
<key>LSItemContentTypes</key>
<array>
<string>com.adobe.pdf</string>
</array>
<key>LSHandlerRank</key>
<string>Default</string>
</dict>
<dict>
<key>CFBundleTypeName</key>
<string>JPEG Image</string>
<key>LSItemContentTypes</key>
<array>
<string>public.jpeg</string>
</array>
<key>LSHandlerRank</key>
<string>Default</string>
</dict>
<dict>
<key>CFBundleTypeName</key>
<string>PNG Image</string>
<key>LSItemContentTypes</key>
<array>
<string>public.png</string>
</array>
<key>LSHandlerRank</key>
<string>Default</string>
</dict>
<dict>
<key>CFBundleTypeName</key>
<string>Microsoft Word Document</string>
<key>LSItemContentTypes</key>
<array>
<string>org.openxmlformats.wordprocessingml.document</string>
<string>com.microsoft.word.doc</string>
</array>
<key>LSHandlerRank</key>
<string>Default</string>
</dict>
<dict>
<key>CFBundleTypeName</key>
<string>Microsoft PowerPoint Presentation</string>
<key>LSItemContentTypes</key>
<array>
<string>org.openxmlformats.presentationml.presentation</string>
<string>com.microsoft.powerpoint.ppt</string>
</array>
<key>LSHandlerRank</key>
<string>Default</string>
</dict>
</array>
</dict> </dict>
</plist> </plist>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment