確定這是我的看法
@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>
}
這是我的視圖模型
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; }
}
這是我的控制器動作。
[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.
這是MVC代碼。如果您使用的是Web窗體,那麼代碼應該更短。 我從鏈接中得到了這個,但現在找不到它,所以只是發佈了我自己的代碼。您還需要確保使用Cpanel在您的主機中啓用了寫入權限。
如果您在IIS上運行它,看看http://stackoverflow.com/questions/4877741/access-to-the-path-is-denied –
有寫啓用服務器Cpanel上的權限?我在Arvixe託管我的同樣的問題。如果您使用代碼在沒有FTP的情況下上傳,則需要啓用寫入權限。 –