我试图按照this answer使用Jquery通过Ajax和PHP上传文件,但它不起作用。
这是我的HTML表单的一部分(我使用bootstrap):
<form role="form" enctype="multipart/form-data" id="edit-company" method="POST">
<div class="form-group">
<div class="row">
<div class="col-md-6">
<div class="row">
<div class="col-md-12 form-group">
<label for="field-name-company">Name:</label> <input id="field-name-company" name="nameCompany" class="form-control" type="text"/>
</div>
</div>
<div class="row">
<div class="col-md-12 form-group">
<label for="field-street-social-emp">Street</label> <input id="field-street-social-emp" name="streetCompany" class="form-control" type="text"/>
</div>
</div>
<div class="col-md-12 form-group">
<div class="row">
<label for="foto-empresa">Foto/Logo:</label>
<div class="form-group">
<input type="file" name="fotoEmpresa"/>
</div>
<button class="btn btn-success pull-right" type="submit" value="Confirm" id="submit-edit-company"><span class="glyphicon glyphicon-ok"></span> Update</button>
</div>
</div>
</div>
</div>
</div>
</form>
这是我的jquery片段:
$('#submit-edit-company').click(function(e) {
e.preventDefault();
var formToSubmit = $(this).parents('form#edit-company');
var dataToSend = formToSubmit.serialize();
var fileInput = formToSubmit.find('input[type="file"]')[0];
var fileData = fileInput.attr('files')[0];
console.log(fileData);
$.ajax({
url: 'app/editCompanyService',
data: dataToSend,
type: 'POST',
dataType: 'JSON',
contentType: false,
cache: false,
processData:false,
beforeSend: function () {},
success: function (return) {},
error: function(jqXHR, textStatus, errorThrown) {
console.log("Error... " + textStatus + " " + errorThrown);
}
当我调试javascript并监控fileInput
时,我可以看到Files: FilesList[1] 0: File...
这样的文件,但脚本只是停止并且不会在控制台中显示任何内容,甚至错误。
PHP中的$_FILES
var只是空的。 (this post中指向的所有配置已经检查过。)
还尝试了fileInput.prop('files')[0];
和new FormData(formToSubmit)
,但我遇到了同样的问题。
我做错了什么?
答案 0 :(得分:0)
虽然不是同一个问题,this SO POST的答案(在@u_mulder的问题评论中链接)解决了我的问题。
以下是功能代码:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSMutableData *body = [NSMutableData data];
NSString *boundary = @"---------------------------14737809831466499882746641449";
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
[request addValue:contentType forHTTPHeaderField: @"Content-Type"];
NSData *imageData = UIImageJPEGRepresentation(image,0.5);//or you can use png representation- UIImagePNGRepresentation(image);
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Disposition: form-data; name=\"thumbnail\"; filename=\"image.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/png\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imageData];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// parameter all_data
[body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"all_data\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"%@",dict] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
// close form
[body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the request
[request setHTTPBody:body];
NSError *error=nil;
NSHTTPURLResponse *response=nil;
NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];