我无法在我的MVC应用程序中上传图片。有谁知道如何解决这个错误?
模特图片:
jsonSerialize
索引视图:
namespace HelloWorld.Models
{
public class Image
{
[Required]
[DataType(DataType.MultilineText)]
public string ImagePath { get; set; }
public int ImageId { get; set; }
}
}
创建视图:
@model IEnumerable<HelloWorld.Models.Image>
@{
ViewBag.Title = "Index";
}
<p>
@Html.ActionLink("Create New", "Create")
</p>
@foreach (var item in Model) {
<img src="@Html.DisplayFor(modelItem => item.ImagePath)">
}
创建操作:
@model HelloWorld.Models.Image
@{
ViewBag.Title = "Create";
}
<form method="post" action="@Url.Action("Create", "Images")">
<div>
<h4>Image</h4>
<hr />
<input type="file" name="file">
<input type="submit" value="Upload">
</div>
</form>
<div>
@Html.ActionLink("Back to List", "Index")
</div>
我认为我的问题是我的模型状态无效。因为我有RedirectToAction(&#34;创建&#34;,&#34;图像&#34;);
答案 0 :(得分:1)
发布文件时,您需要在表单上包含multipart / form-data enctype。
<?php
$target_dir = "admin/upload/".$_POST['tab']."/";
// $target_dir ="/domains/i310033.iris.fhict.nl/public_html/SmokeStik/admin/upload/".$_POST['tab']."/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
echo "File is not an image.";
$uploadOk = 0;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 1000000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["name"], $target_file)) {
echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
echo "Sorry, there was an error uploading your file.";
}
}
include 'connection.php';
$titel = $_POST['titel'];
$imagelink = $target_dir.$_FILES["fileToUpload"]["name"];
$omschrijving = $_POST['content'];
$prijs = $_POST['prijs'];
$tab = $_POST['tab'];
$sql = "INSERT INTO $tab (titel, imagelink, omschrijving, prijs, tab)
VALUES ('$titel', '$imagelink', '$omschrijving', '$prijs', '$tab')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
echo $imagelink;
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
<强>更新强>
为您的文件创建视图模型
<form method="post" action="@Url.Action("Create", "Images")" enctype="multipart/form-data">
<div>
<h4>Image</h4>
<hr />
<input type="file" name="file">
<input type="submit" value="Upload">
</div>
</form>
然后按以下步骤更新您的操作
public class FileViewModel
{
[Required]
public HttpPostedFileBase file { get; set; }
}