这篇文章主要介绍了C# 中实现ftp 图片上传功能(多快好省),需要的朋友可以参考下
前言
此篇讲到的是图片上传功能,每个网站必定会有这样类似的功能,上传文件、上传图片等等。那么接下来,看看我们EF+uploadfile+ftp如何玩转上传图片吧
效果预览
具体实现
一个简单数据库 只有一个主键Id,一个身份证正面路径和一个身份证背面路径三个字段。
首先呢,我们把实体类新建好如下:
public class ImageModel:BaseEntity { /// <summary> /// 用户Id /// </summary> public int ID { get; set; } /// <summary> ///身份证正面相对路径 /// </summary> public string IDProofFront { get; set; } /// <summary> ///身份证背面相对路径 /// </summary> public string IDProofBack { get; set; } }
其中 我们将身份信息实体继承自BaseEntity,我们看看BaseEntity里面是什么东东,代码如下:
public abstract partial class BaseEntity { public override bool Equals(object obj) { return Equals(obj as BaseEntity); } private Type GetUnproxiedType() { return GetType(); } public virtual bool Equals(BaseEntity other) { if (other == null) return false; if (ReferenceEquals(this, other)) return true; return false; } public override int GetHashCode() { return base.GetHashCode(); } public static bool operator ==(BaseEntity x, BaseEntity y) { return Equals(x, y); } public static bool operator !=(BaseEntity x, BaseEntity y) { return !(x == y); } }
这里,我们将BaseEntity定义成一个抽象类,里面包含一些静态方法和重载方法
======================回到HTML=======
我们先回过头来讲页面,上面演示的是一个很简单的单页面,HTML代码如下:
<form enctype="multipart/form-data" id="form" action="/Home/UpLoadImage" method="post"> <p class="full_w" style="margin-top: 100px; margin-left: 30%; width: 800px;"> <p class="h_title"> <b>用户上传的文件</b></p> <p class="entry"> 步骤: <span class="red" style="color: red">(上传资料必须是bmp,gif,jpg,jpeg,png类型,不能大于2M)</span> <ol> <li>先按『选择』键选择上传文件;</li> <li>按『上传』键上传文件;</li> <li>按『保存』键保存文件;</li> </ol> </p> <p class="entry"> <p class="sep"></p> </p> <p class="entry"> <p id="wrapper" style="text-align: center; position: relative;"> <p class="form-group"> <input id="uploadfile" type="file" value="浏览..." class="file" name="FileName" data-upload-url="#" style="position: absolute; top: 0; right: 0; min-width: 100%; min-height: 100%; text-align: right; opacity: 0; background: none repeat scroll 0 0 transparent; cursor: inherit; display: block;" /> </p> </p> </p> <table> <tbody> <tr> <td class="entry">身份证正面</td> <td> @if (Model == null || Model.ID == null || string.IsNullOrEmpty(Model.IDProofFront)) { <a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" target="_blank" class="winView"> <img style="border: none; width: 150px; height: 100px" src="/img/noupload.png" /> </a> } else { <a href=&quo<em style="color:transparent">本文来源[email protected]搞@^&代*@码网(</em>t;@(ViewBag.pathSrc + Model.IDProofFront)" rel="external nofollow" target="_blank"class="winView" > <img style="border: none; width: 150px; height: 100px" src="@(ViewBag.pathSrc + Model.IDProofFront)" /> </a> } @Html.HiddenFor(m => m.IDProofFront) @Html.HiddenFor(m => m.ID) </td> <td> <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" class="easyui-linkbutton btnFinleUP" data-op="1" data-type="image">上传</a> </td> </tr> <tr> <td class="entry">身份证背面</td> <span id="lblinfosi" style="color: Green"></span> <td> @if (Model == null || Model.ID == null || string.IsNullOrEmpty(Model.IDProofBack)) { <a href="javascript:void(0);" rel="external nofollow" rel="external nofollow" target="_blank" class="winView"> <img style="border: none; width: 150px; height: 100px" src="/img/noupload.png" /> </a> } else { <a href="@(ViewBag.pathSrc + Model.IDProofBack)" rel="external nofollow" target="_blank" class="winView" > <img style="border: none; width: 150px; height: 100px" src="@(ViewBag.pathSrc + Model.IDProofBack)" /> </a> } @Html.HiddenFor(m => m.IDProofBack) </td> <td> <a href="javascript:void(0)" rel="external nofollow" rel="external nofollow" class="easyui-linkbutton btnFinleUP" data-op="2" data-type="image">上传</a> </td> </tr> </tbody> </table> <p class="entry"> <button class="button" name="btnSaveAll" value="保存" id="btnSaveAll" style="height: 30px; width: 80px; text-align: center;">保存</button> <a href="/Home/Index" rel="external nofollow" style="height: 30px; text-align: center; width: 80px; background: #ffffff; border: 1px solid #DCDCDC; border-radius: 2px; color: #444444; cursor: pointer; display: inline-block; font: 700 11px Tahoma, Arial, sans-serif; margin-right: 10px; padding: 7px 12px 7px 12px; position: relative; text-decoration: none; text-shadow: 0px 1px 0px #FFFFFF;">返回</a> </p> </p> </form>