首先引入这两个
<script src="@Url.Content("~/uploadify/jquery.uploadify.js")" type="text/javascript"></script>
<link href="@Url.Content("~/uploadify/uploadify.css")" rel="stylesheet" type="text/css" />
下载地址 http://download.csdn.net/detail/qq931121347/6815115
上传的JS·
$(document).ready(function () {
$('#file1').uploadify({
auto: true,
buttonText: '选择图片',
swf: '@Url.Content("~/uploadify/uploadify.swf")',
uploader: 'uploadFile',
fileTypeDesc: '图片文件(*.jpg,*.png,*.gif)',
fileTypeExts: '*.jpg;*.png;*.gif',
onUploadSuccess: function (file, data, response) { $("#picname").val(data); $("#pic").attr("src", "/uploadfile/images/" + data); }
});
});
</script>
<input type="file" name="file1" id="file1"/>
隐藏域
<input id="picname" name="picname" type="hidden" /><br />
预览
<img id="pic" src="" height="150px;" width="150px" />
Controller
#region 上传图片
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult uploadFile(HttpPostedFileBase FileData)
{
string filename = Guid.NewGuid().ToString("N") + Path.GetExtension(FileData.FileName);
string folder = Server.MapPath("/uploadfile/images/");
if (null != FileData)
{
try
{
string fullPathname = Path.Combine(folder, filename);
saveFile(FileData, folder, filename);
}
catch (Exception ex)
{
filename = ex.ToString();
}
}
return Content(filename);
}
#endregion