【问题标题】:How to limit upload file type/format in GCDWebUploader如何在 GCDWebUploader 中限制上传文件类型/格式
【发布时间】:2019-10-14 15:05:24
【问题描述】:

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

我在 GCDWebUploader.m 中找到了这个 uploadFile 方法,也许这就是我需要的,在另一个类中覆盖这个方法。但它在 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,可能用于上传警告的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);
      }
    },

【问题讨论】:

    标签: swift gcdwebserver


    【解决方案1】:

    好吧,我今天找到了方法。很简单,只要定义allowedFileExtensionsproperty,在GCDWebUploader中提供。在我的情况下,如下代码片段。

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

    // 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")
            }
        }
    

    【讨论】:

      猜你喜欢
      • 2011-11-11
      • 2010-12-16
      • 2016-04-11
      • 2016-09-25
      • 1970-01-01
      • 1970-01-01
      • 2010-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多