单文件上传-asp.net mvc版本

1.web.config
appSettings配置节
<add key="UploadFileUrl" value="http://file.mydomain.com/" />
<add key="UploadFilePath" value="E:\file.mydomain.com\imagefiles\"/>

2.Controller
//FileController.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Drawing;
using System.Configuration;

namespace MyDomain.Controllers
{
    public class FileController : Controller
    {
        public ActionResult Up()
        {
            return View();
        }

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Up(HttpPostedFileBase postedFile)
        {
            if (postedFile != null)
            {
                string dir = ConfigurationManager.AppSettings["UploadFilePath"];//Server.MapPath("/imagefiles/");
  string rooturl = System.Configuration.ConfigurationManager.AppSettings["rooturl"];
                string guid = System.Guid.NewGuid().ToString();
                string ext = postedFile.FileName.Substring(postedFile.FileName.LastIndexOf("."));
                string filename = dir + guid + ext;
                string url =  rooturl + "imagefiles/" + guid + ext;
                postedFile.SaveAs(filename);

                ViewData["url"] = url;
            }
            else
            {
                ViewData["msg"] = "posted file is null.";
                ViewData["url"] = "";
            }
            return View();
        }
     }
}

3.View
File/Up.aspx

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Upload File</title>
    <script type="text/javascript" src="/scripts/myjsframe.js"></script>
    <script type="text/javascript">
        function closeWindow() {
            window.returnValue = result.innerText;
            window.close();
        }
    </script>
    <style type="text/css">
        body
        {
            font-size: .75em;
            font-family: Verdana, Helvetica, Sans-Serif;
            margin: 0;
            padding: 0;
            color: #696969;
        }

        a:link
        {
            color: #034af3;
            text-decoration: underline;
        }
        a:visited
        {
            color: #505abc;
        }
        a:hover
        {
            color: #1d60ff;
            text-decoration: none;
        }
        a:active
        {
            color: #12eb87;
        }
       
        #form_content{ list-style:none; }
        #form_content li{ margin-bottom:10px; }
        #form_content li table tr{ margin-bottom:10px; }
       
        #footer{ text-align:center;}
    </style>
    <base target="_self" />
</head>
<body>
    <div>
        <h2>Upload File</h2>
        <ul id="form_content">
            <li>
                You may upload image file,such as JPEG,BMP,GIF,PNG.
            </li>
            <li>
                <% using (Html.BeginForm("Up", "File", FormMethod.Post, new { id = "uploadForm", enctype = "multipart/form-data" }))
                   {
                %>
                    <table width="98%" >
                        <tr><td width="10%">File:</td><td><input type="file" name="postedFile" style="width:80%" />&nbsp;&nbsp;<input type="submit" value="Upload" /></td></tr>
                        <tr><td>Result:</td><td ><%=ViewData["msg"] %><span id="result" runat="server"><%=ViewData["url"] %></span></td></tr>
                        <tr><td  colspan="2" align="center"><input type="button" value="Close" onclick="closeWindow()" /></td></tr>
                    </table>
                <%} %>
            </li>
        </ul>
        <div id="footer">
           
        </div>
    </div>
</body>
</html>


4.在页面中使用以上文件上传页面
<script type="text/javascript">
 function uploadFile(obj) {
            var url = "/file/up";
            var result = window.showModalDialog(url + "?R=" + Math.random());
            if (result != undefined && result != "") {
  var inputObj = document.getElementById(obj);
                inputObj.value = result;
            }
        }

 function showPic(obj) {
     var inputObj = document.getElementById(obj);
            var url = inputObj.value;
     if(url == "") return;
            window.open(url);
        }
</script>

<input type="text" name="image1" /> <a href='javascript:void(0)' onclick="uploadFile('image1')">Up</a> | <a href='javascript:void(0)' onclick="showPic('image1')">View</a>

当然了,也可以把这个改写成一个控件,来调用,就跟asp.net那个版本一样。

posted @ 2009-09-29 21:29  XuShop  阅读(417)  评论(0编辑  收藏  举报