簡體   English   中英

如何在 GCDWebUploader 中限制上傳文件類型/格式

[英]How to limit upload file type/format in GCDWebUploader

我的應用程序涉及將音頻文件上傳到 iPhone 沙箱的 GCDWebUploader。 它運行良好,但我想限制用戶可以上傳的上傳文件類型/格式,例如禁止上傳 xxx.doc、xxx.jpg。 目前我只想允許.MP3 和.AAC 格式。

我在 GCDWebUploader.m 中找到了這個uploadFile方法,也許這就是我需要的,在另一個 class 中覆蓋這個方法。 但它在 objective-C 中,我如何在 Swift 中使用它? 另外我需要在htmljs文件中進行相應更改。 歡迎任何幫助和提示。

///GCDWebUploader.m中的uploadFile方法

- (GCDWebServerResponse*)uploadFile:(GCDWebServerMultiPartFormRequest*)request {
  NSRange range = [[request.headers objectForKey:@"Accept"] rangeOfString:@"application/json" options:NSCaseInsensitiveSearch];
  NSString* contentType = (range.location != NSNotFound ? @"application/json" : @"text/plain; charset=utf-8");  // Required when using iFrame transport (see https://github.com/blueimp/jQuery-File-Upload/wiki/Setup)

  GCDWebServerMultiPartFile* file = [request firstFileForControlName:@"files[]"];
  if ((!_allowHiddenItems && [file.fileName hasPrefix:@"."]) || ![self _checkFileExtension:file.fileName]) {
    return [GCDWebServerErrorResponse responseWithClientError:kGCDWebServerHTTPStatusCode_Forbidden message:@"Uploaded file name \"%@\" is not allowed", file.fileName];
  }
  NSString* relativePath = [[request firstArgumentForControlName:@"path"] string];
  NSString* absolutePath = [self _uniquePathForPath:[[_uploadDirectory stringByAppendingPathComponent:GCDWebServerNormalizePath(relativePath)] stringByAppendingPathComponent:file.fileName]];

  if (![self shouldUploadFileAtPath:absolutePath withTemporaryFile:file.temporaryPath]) {
    return [GCDWebServerErrorResponse responseWithClientError:kGCDWebServerHTTPStatusCode_Forbidden message:@"Uploading file \"%@\" to \"%@\" is not permitted", file.fileName, relativePath];
  }

/// index.html中的alert,可能用於上傳警告的提示。

    <script type="text/x-tmpl" id="template-alert">
      <div class="alert alert-{%=o.level%} alert-dismissable">
        <button type="button" class="close" data-dismiss="alert">&times;</button>
        <strong>{%=o.title%}</strong>{%=o.description%}
      </div>
    </script>

///index.js中的代碼

    fail: function(e, data) {
      var file = data.files[0];
      if (data.errorThrown != "abort") {
        _showError("Failed uploading \"" + file.name + "\" to \"" + _path + "\"", data.textStatus, data.errorThrown);
      }
    },

好吧,我今天找到了如何做到這一點。 很簡單,只需要定義 GCDWebUploader 中提供的allowedFileExtensions屬性即可。 在我的情況下,如下代碼片段。

///在自己的class中定義這個屬性,那么這個問題就解決了。

// create webUploader, access with your iPhone's IP from computer's browser.
    static func initWebUploader() {
        webUploader.start()
        webUploader.allowedFileExtensions = ["aac", "mp3"]
        if webUploader.serverURL != nil {
            print("Visit \(webUploader.serverURL!) in your web browser")
        } else {
            print("You are not connected with Wifi")
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM