這幾天練習的ASP.NET MVC項目中碰到了檔案上傳的問題,分享一下:
View:
{
this.style.display='none'; document.getElementById('Code_Closed_Text_145457').style.display='none'; document.getElementById('Code_Open_Image_145457').style.display='inline'; document.getElementById('Code_Open_Text_145457').style.display='inline';
}" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" alt="" width="11" height="16" align="top">{
this.style.display='none'; document.getElementById('Code_Open_Text_145457').style.display='none'; getElementById('Code_Closed_Image_145457').style.display='inline'; getElementById('Code_Closed_Text_145457').style.display='inline';
}" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" alt="" width="11" height="16" align="top">View
1<form action="/AdminImage/Index/" method="post" enctype="multipart/form-data">
2 <div>
3 Please select a file to upload:
4 <input type="file" id="fileImage" name="fileImage" />
5 <input type="submit" value="Upload" />
6 </div>
7</form>
Controller:
{
this.style.display='none'; document.getElementById('Code_Closed_Text_145657').style.display='none'; document.getElementById('Code_Open_Image_145657').style.display='inline'; document.getElementById('Code_Open_Text_145657').style.display='inline';
}" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" alt="" width="11" height="16" align="top">{
this.style.display='none'; document.getElementById('Code_Open_Text_145657').style.display='none'; getElementById('Code_Closed_Image_145657').style.display='inline'; getElementById('Code_Closed_Text_145657').style.display='inline';
}" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" alt="" width="11" height="16" align="top">Controller
1 public ActionResult Index()
2{
this.style.display='none'; document.getElementById('Codehighlighter1_44_82_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_44_82_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_44_82_Closed_Text').style.display='inline';
}" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" alt="" align="top">{
this.style.display='none'; document.getElementById('Codehighlighter1_44_82_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_44_82_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_44_82_Open_Text').style.display='inline';
}" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" alt="" align="top"> {
3 return View();
4 }
5 [AcceptVerbs(HttpVerbs.Post)]
6 public ActionResult Index(FormCollection collection)
7{
this.style.display='none'; document.getElementById('Codehighlighter1_190_745_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_190_745_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_190_745_Closed_Text').style.display='inline';
}" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif" alt="" align="top">{
this.style.display='none'; document.getElementById('Codehighlighter1_190_745_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_190_745_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_190_745_Open_Text').style.display='inline';
}" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif" alt="" align="top"> {
8 if (Request.Files.Count == 0)
9{
this.style.display='none'; document.getElementById('Codehighlighter1_246_292_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_246_292_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_246_292_Closed_Text').style.display='inline';
}" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" alt="" align="top">{
this.style.display='none'; document.getElementById('Codehighlighter1_246_292_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_246_292_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_246_292_Open_Text').style.display='inline';
}" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" alt="" align="top"> {
10 return View();
11 }
12 var c = Request.Files[0];
13 if (c != null && c.ContentLength > 0)
14{
this.style.display='none'; document.getElementById('Codehighlighter1_393_708_Open_Text').style.display='none'; document.getElementById('Codehighlighter1_393_708_Closed_Image').style.display='inline'; document.getElementById('Codehighlighter1_393_708_Closed_Text').style.display='inline';
}" src="http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif" alt="" align="top">{
this.style.display='none'; document.getElementById('Codehighlighter1_393_708_Closed_Text').style.display='none'; document.getElementById('Codehighlighter1_393_708_Open_Image').style.display='inline'; document.getElementById('Codehighlighter1_393_708_Open_Text').style.display='inline';
}" src="http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif" alt="" align="top"> {
15 int lastSlashIndex = c.FileName.LastIndexOf("//");
16 string fileName = c.FileName.Substring(lastSlashIndex + 1, c.FileName.Length - lastSlashIndex - 1);
17 fileName = Path.Combine(CommonUtility.DocImagePath, fileName);
18 c.SaveAs(fileName);
19 }
20 return View();
21 }