我只是有写脚本发送电子邮件。它接收随机电子邮件,并使用随机值发送-一切都很好。现在我只想添加选项来发送多个图像-随机选择一个,并发送电子邮件。
我使用bootstrap,我找到了这个插件:http://plugins.krajee.com/file-input
但是当我尝试上传图片时,我得到了405错误。顺便问一下,你知道用ajax发送附件的其他方法吗?
下面是我的代码:
$("#dodawarka").submit(function(e){
e.preventDefault();
var tytul = $("#tytul").val();
var kontos = $("#lkonta").val();
var tresc = $("#tresc").val();
var jemail = $("#kemaile").val();
var jemail = $("#kemaile").val();
var dataString = 'listakont=' + kontos + '&tytul=' + tytul + '&tresc=' + tresc + '&emailjaki=' + jemail;
$.ajax({
type: "POST",
url: "dodaj.php",
data: dataString,
beforeSend: function(){
$('#tytul, #tresc, #przyciskx').prop( "disabled", true );
$('.wczytywanie').show();
},
success: function(){
$('#ddodaneok').show();
$('#dodawarka').slideUp();
$('#linkizwrotne').show();
},
error: function(){
$('#kurwasabledy').show();
}
});
return false;
});和:
<form role="form" enctype="multipart/form-data" id="dodawarka" method="post">
<div class="form-group" id="listakont" style="visibility:hidden; position: absolute;">
<label for="listakont">Lista kont na które zostaną wysłane wpisy</label>
<textarea class="form-control" rows="10" id="lkonta" disabled></textarea>
</div>
<div class="form-group" id="ssssemail" style="visibility:hidden; position: absolute;">
<label for="ssssemail">Emaile z których będzie można wysyłam wpisy podaj w formacie</label>
<textarea class="form-control" rows="10" id="kemaile"></textarea>
</div>
<div class="form-group">
<label for="tytul">Podaj tytuł wpisu <em>+spintax</em></label>
<input type="tytul" class="form-control" id="tytul" required>
</div>
<div class="form-group">
<label for="tresc">Treść wpisu <em>+spintax</em></label>
<textarea class="form-control" rows="25" id="tresc" required></textarea>
</div>
<div class="form-group">
<label for="obrazki">Dodaj obrazki</label>
<input id="input-id" type="file" class="file" data-preview-file-type="text" multiple=true data-min-file-count="1">
</div>
<button type="submit" class="btn btn-default" id="przyciskx"><img src="img/infinity.gif" class="wczytywanie" /> Wyślij na zaplecza</button>
</form>发布于 2016-08-14 19:23:47
尝试:
<input id="input-id" type="file" class="file" data-preview-file-type="text" multiple name="files[]" data-min-file-count="1">
添加了:name="files[]"和only multiple而不是multiple=true。
在php中这样做:
$output = '';
if(is_array($_FILES)){
foreach($_FILES['files']['name'] as $name => $value)
{
$file_name = explode(".", $_FILES['files']['name'][$name]);
$allowed_ext = array("jpg", "jpeg", "png", "gif");
if(in_array($file_name[1], $allowed_ext))
{$new_name = $userid.md5(rand()).".".$file_name[1];
$sourcePath = $_FILES['files']['tmp_name'][$name];
$targetPath = "uploadedimages/".$new_name;
include_once("connection.php"); // your connection to DB and select DB
$sql = "INSERT into images(userid, imgpath) VALUES ('$userid', '$targetPath')";
$query = $conn->query($sql);
if($query && move_uploaded_file($sourcePath, $targetPath))
{
$output .= '<div class="col-md-3"><img src="'.$targetPath.'" class="img-thumbnail" width="150px" height="180px"/></div>';
}
}
}
}
echo $output;https://stackoverflow.com/questions/38941647
复制相似问题