Ok đây là quan điểm của tôi
@using (Html.BeginForm("Upload", "Pictures", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
@Html.AntiForgeryToken()
<div>
Title:<br/>
@Html.EditorFor(x => x.Title)<br/>
@Html.ValidationMessageFor(x => x.Title)<br/>
@Html.TextBoxFor(x => x.File, new {
type = "file"
})<br/>
@Html.ValidationMessageFor(x => x.File)<br/>
Description:<br/>
@Html.TextAreaFor(x => x.Description)<br/>
@Html.ValidationMessageFor(x => x.Description)
</div>
<div style="clear:both"></div>
<p><input type="submit" value="Save"/></p>
}
Đây là quan điểm mô hình của tôi
public class UploadModel
{
[Required(ErrorMessage=("You have not selected a file"))]
public HttpPostedFileBase File { get; set; }
[Required(ErrorMessage = "Please enter a title")]
[StringLength(50)]
public string Title { get; set; }
[StringLength(400)]
public string Description { get; set; }
}
Đây là hành động điều khiển của tôi.
[Authorize(Roles = "Approved")]
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Upload(UploadModel m)
{
byte[] uploadedFile = null;
Byte123 xxx = new Byte123();
if (m.File != null && !string.IsNullOrEmpty(m.Title))
{
//var fileName = System.IO.Path.GetFileName(m.File.FileName);
//string c = m.File.FileName.Substring(m.File.FileName.LastIndexOf("."));
// m.Title = m.Title.Replace(c, "");
uploadedFile = new byte[m.File.InputStream.Length]; //you get the image as byte here but you can also save it to file.
Đây là mã MVC. Nếu bạn đang sử dụng Biểu mẫu web thì mã sẽ ngắn hơn. Tôi nhận được thông tin này từ một liên kết nhưng không thể tìm thấy nó ngay bây giờ để chỉ đăng mã của riêng tôi. Bạn cũng cần đảm bảo rằng quyền Ghi được bật trong máy chủ của bạn bằng Cpanel.
Nguồn
2013-10-20 10:55:26
Nếu bạn chạy nó trên IIS, hãy xem http://stackoverflow.com/questions/4877741/access-to-the-path-is-denied –
đã ghi được kích hoạt quyền trên máy chủ Cpanel? Tôi đang lưu trữ mỏ của tôi ở Arvixe và có cùng một vấn đề. Bạn cần bật quyền ghi nếu bạn đang sử dụng mã để tải lên mà không cần FTP. –