一网通办ios
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

fileSelect.swift 1.6 KiB

2 år sedan
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. //
  2. // ContentView.swift
  3. // fileModel
  4. //
  5. // Created by 陈春杨 on 2022/2/16.
  6. //
  7. import SwiftUI
  8. struct ContentView: View {
  9. var body: some View {
  10. Button {
  11. let path = loadFile()
  12. } label: {
  13. HStack {
  14. Image("upload").resizable().frame(width: 15, height: 15)
  15. Text("上传文件")
  16. .foregroundColor(Color.white)
  17. .font(Font.system(size: 14))
  18. }.frame(width: 106, height: 32)
  19. .background(Color.red)
  20. }.buttonStyle(PlainButtonStyle())
  21. .cornerRadius(4)
  22. .padding(EdgeInsets(top: 29, leading: 0, bottom: 29, trailing: 0)) }
  23. // 文件选择
  24. func loadFile(isFolder: Bool = false, openFinder: String = "", fileTypes: [String] = [String]()) -> String {
  25. let dialog = NSOpenPanel()
  26. dialog.title = "选择文件"
  27. dialog.showsResizeIndicator = true
  28. dialog.showsHiddenFiles = false
  29. dialog.allowsMultipleSelection = false
  30. dialog.canChooseDirectories = isFolder
  31. dialog.canChooseFiles = !isFolder
  32. if !fileTypes.isEmpty {
  33. dialog.allowedFileTypes = fileTypes
  34. }
  35. if !openFinder.isEmpty, let url = URL(string: openFinder) {
  36. dialog.directoryURL = url
  37. }
  38. if dialog.runModal() == .OK, let path = dialog.url?.path {
  39. return path
  40. } else {
  41. return ""
  42. }
  43. }
  44. }
  45. struct ContentView_Previews: PreviewProvider {
  46. static var previews: some View {
  47. ContentView()
  48. }
  49. }