• MVC3 上传文件【转】


    前台引擎采用Razor

    上传页View:

    @model System.Web.HttpContextBase
    @{
    ViewBag.Title = "上传文件";
    }

    <h2>上传文件</h2>
    <br />
    <br />
    @*new { enctype = "multipart/form-data" }比不可少,否则上传文件不会成功 *@
    @using (Html.BeginForm("Upload", "UploadFile", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
    <text>选择上传文件:</text><input name="file" type="file" id="file" />
    <br />
    <br />
    <input type="submit" name="Upload" value="Upload" />
    }

    你的后台UploadFileController:

            [HttpPost]
    public ActionResult Upload(FormCollection form)
    {
    if (Request.Files.Count == 0)
    {
          //Request.Files.Count 文件数为0上传不成功
          Return View(); 
         }

    var file = Request.Files[0];
    if (file.ContentLength == 0)
    {
    //文件大小大(以字节为单位)为0时,做一些操作
          Return View();
         }
         else
        {
          //文件大小不为0
          HttpPostedFileBase file = Request.Files[0];
          //保存成自己的文件全路径,newfile就是你上传后保存的文件,
          
    //服务器上的UpLoadFile文件夹必须有读写权限      
          file.SaveAs(Server.MapPath(@"UploadFile\newfile"));
        }
         
         
          
         newFile = DateTime.Now.ToString("yyyyMMddHHmmss") + ".sl";

    return View();

    }

    好了,上传后的文件就可以任你操作了

    本文来自:http://www.cnblogs.com/leiOOlei/archive/2011/08/17/2143221.html

  • 相关阅读:
    创建本地源,使用yum install
    查找SCAN大量块的一个sql
    好的代码像首诗,差的代码像坨屎。
    ps
    eclipse程序正确运行却有红叉
    JS中文乱码解决方案
    初学JQuery
    初学JQuery 2
    大神的电脑软件
    eclipse导入已存在于workspace的项目
  • 原文地址:https://www.cnblogs.com/booth/p/2282463.html
Copyright © 2020-2023  润新知