我使用jQuery的ajax函数将文件发送到服务器端PHP脚本时遇到问题.可以获取文件列表$('#fileinput').attr('files')但是如何将此数据发送到服务器?使用文件输入时$_POST,服务器端php脚本上的结果array()为0(NULL).
我知道这是可能的(虽然我到目前为止还没有找到任何jQuery解决方案,只有Prototye代码(http://webreflection.blogspot.com/2009/03/safari-4-multiple-upload-with-progress.html) ).
这似乎是相对较新的,所以请不要通过XHR/Ajax提及文件上传,因为它肯定有用.
我需要Safari 5中的功能,FF和Chrome会很好但不是必需的.
我现在的代码是:
$.ajax({
url: 'php/upload.php',
data: $('#file').attr('files'),
cache: false,
contentType: 'multipart/form-data',
processData: false,
type: 'POST',
success: function(data){
alert(data);
}
});
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 ajax 和 php 上传一些表单数据,但由于某种原因,我的数据没有被捕获或传递。
这是我所拥有的:
form.html(带有 3 个文本输入和 1 个文件的基本表单)
<form class="form" id="superform" action="nada.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="tituloQuiz">Resultado:</label>
<input type="text" class="form-control resultado" id="titulo" name="titulo" placeholder="Ex: Paris">
</div>
<div class="form-group">
<label for="desc">Descrição do Site:</label>
<textarea class="form-control" id="desc" name="descricao" placeholder="Ex:"></textarea>
</div>
<div class="form-group">
<label for="desc">OG FB DESC:</label>
<textarea class="form-control" id="facedes" name="descricao" placeholder="facebook description"></textarea>
</div>
<div class="form-group">
<label for="imggrande">Imagem</label>
<input type="file" id="imggrande" name="imgres">
<p class="help-block">Imagem usada na página de resultado e Facebook 600 x 400.</p>
</div>
<button type="button" class="btn btn-primary …Run Code Online (Sandbox Code Playgroud) 我正在使用summernote,我想将图像上传到我的网络服务器..下面是我正在使用的代码
默认.aspx
<script type="text/javascript">
$(document).ready(function () {
$('#summernote').summernote({
onImageUpload: function (files, editor, $editable) {
alert('image?');
var formData = new FormData();
formData.append("file", files[0]);
$.ajax({
url: "Default.aspx/UploadImage",
data: formData,
type: 'POST',
cache: false,
contentType: false,
processData: false,
success: function (imageUrl) {
alert(imageUrl);
if (!imageUrl) {
// handle error
return;
}
editor.insertImage($editable, imageUrl);
},
error: function () {
alert('error');
// handle error
}
});
console.log('image upload:', files, editor, $editable);
}
});
});
</script>
Run Code Online (Sandbox Code Playgroud)
默认.asp.cs
[System.Web.Services.WebMethod]
public static string UploadImage(HttpPostedFileBase file)
{
//Saving to …Run Code Online (Sandbox Code Playgroud)