我的应用程序涉及将音频文件上传到 iPhone 沙箱的 GCDWebUploader。它运行良好,但我想限制用户可以上传的上传文件类型/格式,例如禁止上传 xxx.doc、xxx.jpg。目前我只想允许 .MP3 和 .AAC 格式。
我uploadFile
在 GCDWebUploader.m 中找到了这个方法,也许这就是我需要的,在另一个类中覆盖这个方法。但它在 Objective-C 中,我如何在 Swift 中使用它?还有我需要在html
andjs
文件中进行相应更改的内容。欢迎任何帮助和提示。
///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">×</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);
}
},