我的应用涉及GCDWebUploader,用于将音频文件上传到iPhone的沙盒。它运行良好,但我想限制用户可以上传的上传文件类型/格式,例如 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];
}
索引中的警报.html,可用于上传警告的警报。
<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>
索引中的代码.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);
}
},
好吧,我今天找到了如何做到这一点。这很简单,只需定义allowedFileExtensions
属性,该属性在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")
}
}