嘗試這個類和下面的操作並修復AppSetting中的文件夾路徑。
配置:
<appSettings>
<add key="UploadFolerPath" value="..Your folder path" />
</appSettings>
觀點: -
<form action="Account/AddImage" id="form_AddImage" method="post" enctype="multipart/form-data">
<input type="file" id="Img" name="Img" class="required" />
<input type="submit" value="Upload" id="btnSubmit" />
</form>
類: -
public class FileUpload
{
public string SaveFileName
{
get;
set;
}
public bool SaveFile(HttpPostedFileBase file, string FullPath)
{
string FileName = Guid.NewGuid().ToString();
FileName = FileName + System.IO.Path.GetExtension(file.FileName);
SaveFileName = FileName;
file.SaveAs(FullPath + "/" + FileName);
return true;
}
}
// POST操作
[HttpPost]
public ActionResult AddImage(FormCollection Form)
{
FileUpload fileupload = new FileUpload();
var image="";
HttpPostedFileBase file = Request.Files["Img"];
if (file.FileName != null && file.FileName != "")
{
if (upload.ContentLength > 0)
{
fileupload.SaveFile(Request.Files["Img"], Server.MapPath(AppSetting.ReadAppSetting("UploadFolerPath")));
image = fileupload.SaveFileName;
// write here your Add/Save function
return Content(image);
}
}
else
{
//return....;
}
}
「沒有人會有一個答案「 - ? – 2009-04-19 10:46:01
那麼給出的2個答案不對,我把錢放在沒有人解決它。 – Malcolm 2009-04-19 11:50:01
答案是解決問題的權利 – Malcolm 2009-04-19 11:50:35