日期:2014-05-17 浏览次数:20452 次
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>11</title> <script type="text/javascript" language="javascript"> function getif(txt1) { var txt = document.getElementById("txt_1"); txt.value = txt1; } </script> </head> <body> <iframe style="display:block;" name="hideframe" id="testframe"></iframe> <div id = "nofri"> <form id = "imageform" method ="post" action= "asp/upload.ashx" enctype ="multipart/form-data" target= "hideframe"> <input type="submit" value="上传"/> <input type="file" name="file" /> <input type="text" id="txt_1" /> <input type="button" id="btn_1" onclick = "getif()" value = "取值"/> </form> </div> </body> </html>
<%@ WebHandler Language="C#" Class="upload" %> using System; using System.Web; using System.IO; using System.Net; public class upload : IHttpHandler { public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/html"; context.Response.ContentType = "utf-8"; HttpPostedFile file = context.Request.Files["file"]; string newFilename = Guid.NewGuid().ToString(); var extension = Path.GetExtension(file.FileName).ToUpper(); newFilename += extension; string filepath = string.Empty; if (SaveAsImg(newFilename, file, context)) { //context.Response.Write("<div id='tframe'>" + newFilename + "</div>"); context.Response.Write("<script>parent.getif('" + newFilename + "')</script>"); } else { context.Response.Write("false"); } } public bool SaveAsImg(string filename, HttpPostedFile file, HttpContext context) { HttpServerUtility server = context.Server; try { string filepath = server.MapPath("~/img/userimg/" + filename); file.SaveAs(filepath); return true; } catch { return false; } } public bool IsReusable { get { return false; } } }