0

我的应用程序涉及将音频文件上传到 iPhone 沙箱的 GCDWebUploader。它运行良好,但我想限制用户可以上传的上传文件类型/格式,例如禁止上传 xxx.doc、xxx.jpg。目前我只想允许 .MP3 和 .AAC 格式。

uploadFile在 GCDWebUploader.m 中找到了这个方法,也许这就是我需要的,在另一个类中覆盖这个方法。但它在 Objective-C 中,我如何在 Swift 中使用它?还有我需要在htmlandjs文件中进行相应更改的内容。欢迎任何帮助和提示。

///GCDWebUploader.muploadFile中的方法

- (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);
      }
    },
4

1 回答 1

0

好吧,我今天找到了如何做到这一点。很简单,只需要定义allowedFileExtensionsGCDWebUploader中提供的属性即可。在我的情况下,如下代码片段。

///在自己的类中定义这个属性,那么这个问题就解决了。

// 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")
        }
    }
于 2019-10-15T08:00:14.663 回答