å¦æåªæ¯ä¸ä¼ çè¯é£å¤ªå®¹æäºï¼å¦æè¿è¦æ¾ç¤ºé£å°±é¾äºï¼å 为è¦æ¾ç¤ºçè¯å°±ä¸è½åªåæå¡å¨æ交ä¸æ¬¡è¯·æ±ï¼å¿
é¡»å¼æ¥æ交ãä¸é¢çä¾åæ¯æ亲èªåçï¼å¼æ¥æ交ä¸ä¼ å¾ç并é¢è§ãå
¨é¨ä»£ç é½å¨ã
é¦å
建ä¸ä¸ªhtmlæ件ï¼å¤å¶ä»¥ä¸htmlææ¬ã使ç¨è¯´æï¼
å¼ç¨jquery两个jsæ件ï¼ç½ä¸èªå·±æå§ï¼å°å¤é½æã
<script src="jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="jquery.form.js" type="text/javascript"></script>
2.æ·»å 两个ææ¬æ¡ï¼ç¬¬ä¸ä¸ªIDå¿
é¡»æ¯âbigImage1âï¼ç¬¬äºä¸ªå¿
é¡»æ¯âsmallbigImage1âã
<input type="text" name="url1" id="bigImage1" style="width:150px;" onclick="selectImage(this)" />
<input type="hidden" name="smallUrl1" id="smallbigImage1" value="" />
å½ç¹å»ç¬¬ä¸ä¸ªææ¬æ¡æ¶ï¼å¼¹åºä¸ä¸ªä¸ä¼ çªå£ï¼éæ©ä¸å¼ å¾ç并ç¹âä¸ä¼ âï¼ä¸ä¼ æååå¯é¢è§å¾çãæ¤è¿ç¨ä¼å¨æå¡å¨ä¸æåå¾ççæä¸å¼ 缩ç¥å¾ï¼å¹¶æåå¾URLå缩ç¥å¾URLä¸èµ·ä»¥JSONæ ¼å¼è¿åå°åå°é¡µé¢ï¼ä¸´æ¶æ¾ç¤ºç¼©ç¥å¾ãå½ç¹å»âç¡®å®âæ¶ï¼å®ä¼æ两个URLæ·»å å°ä¸¤ä¸ªå¯¹åºIDçææ¬æ¡ã第äºä¸ªæ¡æ¯éèçï¼ç»ç¨æ·çæè§å°±åæ¯åªè¿åä¸ä¸ªURLä¸æ ·ã
3.请èªå·±åèæ¬[document.getElementById("bigImage1").value] è·å¾ä¸¤ä¸ªææ¬æ¡çå¼ï¼åè¿è¡ä½ æ³åçæä½ã
4.id为"uploadDiv"çDIVæ¯ä¸ä¸ªæ´ä½ï¼ä¸å¯åå²ã主è¦æ¯æä¾ä¸ä¸ªæ件类åç表åï¼ç¨äºå¼æ¥ä¸ä¼ ã请修æ¹actionçURLä¸ºä½ ä¸ä¼ çåå°è·¯å¾ã
ä¸é¢æ¯HTML代ç
<!DOCTYPE html>
<html>
<head>
<title>Index</title>
<script src="jquery-1.4.2.min.js" type="text/javascript"></script>
<script src="jquery.form.js" type="text/javascript"></script>
<script type="text/javascript">
function selectImage(obj) {
var inputId = obj.id;
document.getElementById('btnSure').setAttribute('inputId', inputId);
var x = window.event.x;
var y = window.event.y;
var uploadDiv = document.getElementById('uploadDiv');
uploadDiv.style.left = x + 10 + 'px';
uploadDiv.style.top = y + 10 + 'px';
uploadDiv.style.position = 'fixed';
uploadDiv.style.display = 'inline';
}
function closeDiv() {
document.getElementById('btnSure').style.display = 'none';
document.getElementById('showImage').style.display = 'none';
document.getElementById('uploadDiv').style.display = 'none';
}
function makeSure(obj) {
var inputId = obj.getAttribute('inputId');
document.getElementById(inputId).value = document.getElementById('showImage').getAttribute('big');
document.getElementById('small' + inputId).value = document.getElementById('showImage').getAttribute('small');
document.getElementById('image' + inputId).src = document.getElementById('showImage').getAttribute('small');
document.getElementById("btnSure").style.display = 'none';
document.getElementById('showImage').style.display = 'none';
document.getElementById('uploadDiv').style.display = 'none';
}
$(function () {
//å¼æ¥ä¸ä¼ å¾ç
$('#btnUpload').click(function () {
if ($('#upImage').val() == '') {
alert('请éæ©ä¸å¼ å¾çæ件');
return;
}
$('#fileForm').ajaxSubmit({
success: function (msg) {
var strJSON = msg; //å¾å°çJSON
var image = eval("(" + strJSON + ")"); //转æ¢åçJSON对象
document.getElementById('uploading').style.display = 'none';
$('#showImage').css('display', 'inline');
$('#showImage').attr('src', image.big);
$('#showImage').attr('big', image.big);
$('#showImage').attr('small', image.small);
$('#btnSure').css('display', 'inline');
}
});
document.getElementById('uploading').style.display = 'block';
});
});
</script>
</head>
<body>
<p>ä¸ä¼ å个æ件æå¾ç</p>
<div>
<input type="text" name="url1" id="bigImage1" style="width:150px;" onclick="selectImage(this)" />
<input type="hidden" name="smallUrl1" id="smallbigImage1" value="" />
</div>
<div id="uploadDiv" style="width: 400px; height: 280px; border: 1px solid #9eb9f1;
background-color: #e1eaea; text-align:left; display:none; z-index:999;">
<div>
<div style="width: 376px; float: left; padding-left:4px; padding-top:4px; padding-bottom:4px; overflow:hidden;">
<form id="fileForm" method="post" action="/Home/UploadImage" enctype="multipart/form-data" >
<input type="file" id="upImage" name="upImage" style="padding-bottom: 1px; padding-left: 8px;
padding-right: 8px; height: 24px; width:220px; padding-top: 1px;" />
<input type="button" id="btnUpload" value="ä¸ä¼ " style="padding-bottom: 1px; padding-left: 8px; padding-right: 8px;
height: 24px; cursor: pointer; padding-top: 1px; line-height: 12px;" />
<span id="uploading" style="color:#ff0000; display:none; font-size:14px; font-weight:bold;">ä¸ä¼ ä¸......</span>
<input type="button" id="btnSure" value="ç¡®å®" style="padding-bottom: 1px; padding-left: 8px; padding-right: 8px;
height: 24px; cursor: pointer; padding-top: 1px; line-height: 12px; display:none; background-color:#9fd0f9;" onclick="makeSure(this)" />
</form>
</div>
<div style="width: 20px; height: 30px; float: right; ">
<div style="width: 20px; height: 20px; background-color:#9fc0f7; font-size:20px; text-align:center; line-height:16px; cursor:pointer;" onclick="closeDiv()">X</div>
</div>
</div>
<div style="width:398px; height:240px; text-align:center; overflow:hidden; ">
<img id="showImage" alt="é¢è§å¾ç" src="" style="width: 340px; display:none;" />
</div>
</div>
</body>
</html>
ä¸é¢æ¯åå°ä»£ç
è¿åå°åå°é¡µé¢çJSONæ ¼å¼å¯¹è±¡æ¯ä»¥ç±»ç对象ã
public class ReturnImage
{
public string big { get; set; }
public string small { get; set; }
public string isSuccessfull { get; set; }
public string message { get; set; }
}
对äºä¸ä¼ åçæ缩ç¥å¾ï¼è¯·èªè¡å®æï¼ä»¥ä¸æ¯ASP.NET MVCçä¾åã
public class HomeController : Controller
{
//
// GET: /Home/
public ActionResult Index()
{
return View();
}
/// <summary>
/// ä¸ä¼ å¾ç
/// </summary>
/// <returns></returns>
public ActionResult UploadImage()
{
//å®ä¹é误æ¶æ¯
JsonResult msg = new JsonResult();
try
{
//æ¥åä¸ä¼ æ件
HttpPostedFileBase postFile = Request.Files["upImage"];
if (postFile != null)
{
DateTime time = DateTime.Now;
//è·åä¸ä¼ ç®å½ 转æ¢ä¸ºç©çè·¯å¾
string uploadPath = Server.MapPath("~/UploadFiles/" + time.Year + "/" + time.ToString("yyyyMMdd") + "/");
//æ件å
string fileName = time.ToString("yyyyMMddHHmmssfff");
//åç¼å称
string filetrype = System.IO.Path.GetExtension(postFile.FileName);
//è·åæ件大å°
long contentLength = postFile.ContentLength;
//æ件ä¸è½å¤§äº2M
if (contentLength <= 1024 * 2048)
{
//å¦æä¸åå¨pathç®å½
if (!Directory.Exists(uploadPath))
{
//é£ä¹å°±å建å®
Directory.CreateDirectory(uploadPath);
}
//ä¿åæ件çç©çè·¯å¾
string saveFile = uploadPath + fileName + "_big" + filetrype;
try
{
//ä¿åæ件
postFile.SaveAs(saveFile);
//ä¿å缩ç¥å¾çç©çè·¯å¾
string small = uploadPath + fileName + "_small" + filetrype;
MakeThumbnail(saveFile, small, 320, 240, "W");
ReturnImage image = new ReturnImage();
image.big = "/UploadFiles/" + time.Year + "/" + time.ToString("yyyyMMdd") + "/" + fileName + "_big" + filetrype;
image.small = "/UploadFiles/" + time.Year + "/" + time.ToString("yyyyMMdd") + "/" + fileName + "_small" + filetrype;
msg = Json(image);
}
catch
{
msg = Json("ä¸ä¼ 失败");
}
}
else
{
msg = Json("æ件大å°è¶
è¿éå¶è¦æ±");
}
}
else
{
msg = Json("请éæ©æ件");
}
}
catch (Exception e)
{
;
}
msg.ContentType = "text/html";
return msg;
}
/// <summary>
ç±äºåçè¶
è¿æ大éå¶ï¼/// çæ缩ç¥å¾ ç代ç 请åæç´¢å