1
我想建立一個圖像應用程序,用戶可以在其中上傳圖像的服務器上。上傳圖像到服務器使用HTML5.0與c#後端
我使用HTML 5.0作爲我的前端,c#作爲我的後端遵循MVC 4架構。我將在下面附上我的代碼。
<div id="imageup">
<form method="post" action="UploadImage" enctype="multipart/form-data">
<label for="file">Filename:</label>
Image: <input type="file" name="file" id="file"/>
NAME: <input type="text" name ="testname" id="nametestid" runat="server"/>
<input type="submit" value="image" />
</form>
</div>
發現這裏是後端代碼:
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult UploadImage(String testname)
{
Console.Out.WriteLine("HERE");
// name.Length
if(testname.Length==0){
System.Console.WriteLine("Hello");
//return Json("All files have been successfully stored.");
}
foreach (string file in Request.Files)
{
HttpPostedFileBase hpf = Request.Files[file] as HttpPostedFileBase;
if (hpf.ContentLength == 0)
continue;
string savedFileName = Path.Combine(
AppDomain.CurrentDomain.BaseDirectory,
Path.GetFileName(hpf.FileName));
hpf.SaveAs(savedFileName);
}
/* foreach (HttpPostedFile file in files)
{
string filePath = Path.Combine(TempPath, file.FileName);
System.IO.File.WriteAllBytes(filePath, ReadData(file.InputStream));
}
//*/
return RedirectToAction("Create");
}
與代碼的問題是,當我通過瀏覽器傳遞文件,並在斷點我得到的圖像的值爲空,但我得到的文本,我已經輸入與數據相同的形式。
由於這是MVC - 獲取'你的名字輸入元素上擺脫了'RUNAT = 「服務器」 的。另外,使'name'和'id'屬性匹配並重試。 – Tommy
我的文件名與文件名相同,文本也不同,但我可以在文本框中輸入文本。當我設置斷點時,我可以看到文本,但文件數量爲0。 –