.net:上传图片并将保存至指定目录下(支持PC端和移动端)

页面:

<body>
  <form id="formid" name="myform" action="Upload" method='post' enctype="multipart/form-data">
    <table>
      <tr>
        <td>图片:</td>
        <td><input type="file" name="file" accept="image/*" value="选择" /></td>
      </tr>
    </table>
    <input type="submit" value="上传" />
  </form>
</body>

后台:

public string Upload()
{
  HttpPostedFileBase file = Request.Files["file"];
  if (file.ContentLength > 0)
  {
    string savePath = Server.MapPath("~/Uploads/Pictures");
    string failPath = savePath + "\\"  + file.FileName;
    file.SaveAs(failPath);
    return “图片上传成功!”;
  }
  else
  {
    return "未选择图片!";
  }
}
posted @ 2016-11-09 19:15  陈昌鸿  阅读(1968)  评论(0编辑  收藏  举报