【发布时间】:2019-10-14 15:05:24
【问题描述】:
我的应用涉及将音频文件上传到 iPhone 沙箱的 GCDWebUploader。它运行良好,但我想限制用户可以上传的上传文件类型/格式,例如xxx.doc、xxx.jpg 禁止上传。目前我只想允许 .MP3 和 .AAC 格式。
我在 GCDWebUploader.m 中找到了这个 uploadFile 方法,也许这就是我需要的,在另一个类中覆盖这个方法。但它在 Objective-C 中,我如何在 Swift 中使用它?我还需要在html 和js 文件中进行相应更改。欢迎任何帮助和提示。
///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">×</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