function upFile
view plaincopy to clipboardprint?
01.function upFile(ob)
02.{
03. var file = document.getElementById(ob) ;
04. var newName = "FileName"; //设置文件保存的名字
05.
06. var form=document.createElement('form');
07. document.body.appendChild(form);
08. form.encoding="multipart/form-data";
09. form.method = "post";
10. form.action= "accept.aspx?nm=" + newName;
11. form.target= "hidden_frame";
12. var pos=file.nextSibling; //记住file在旧表单中的的位置
13. form.appendChild(file);
14. form.submit();
15. pos.parentNode.insertBefore(file,pos);
16. document.body.removeChild(form);
17.}
function upFile(ob)
{
var file = document.getElementById(ob) ;
var newName = "FileName"; //设置文件保存的名字
var form=document.createElement('form');
document.body.appendChild(form);
form.encoding="multipart/form-data";
form.method = "post";
form.action= "accept.aspx?nm=" + newName;
form.target= "hidden_frame";
var pos=file.nextSibling; //记住file在旧表单中的的位置
form.appendChild(file);
form.submit();
pos.parentNode.insertBefore(file,pos);
document.body.removeChild(form);
}
accept.aspx
view plaincopy to clipboardprint?
01.<%@ Page Language="C#" AutoEventWireup="true" CodeFile="up.aspx.cs" Inherits="Member_up" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="up.aspx.cs" Inherits="Member_up" %>
accept.aspx.cs
view plaincopy to clipboardprint?
01.using System;
02.using System.Data;
03.using System.Configuration;
04.using System.Collections;
05.using System.Web;
06.using System.Web.Security;
07.using System.Web.UI;
08.using System.Web.UI.WebControls;
09.using System.Web.UI.WebControls.WebParts;
10.using System.Web.UI.HtmlControls;
11.
12.
13.public partial class Member_up : System.Web.UI.Page
14.{
15. protected void Page_Load(object sender, EventArgs e)
16. {
17. string mz = HttpContext.Current.Request.QueryString["nm"].ToString();
18. string uperr = "";
19. HttpFileCollection files = HttpContext.Current.Request.Files;
20.
21. if (files.Count>0)
22. { uperr = upSingleFile(files[0], mz); }
23. else { uperr = "ok"; }
24. HttpContext.Current.Session["upInfo"] = uperr;
25. Response.Write(uperr);
26. }
27.
28.
29. //上传文件
30. private string upSingleFile(HttpPostedFile file, String theFileName)
31. {
32. string infos = "";
33. bool fileOK = false;
34.
35. string fileName, fileExtension ;
36. fileName = System.IO.Path.GetFileName(file.FileName);
37. if (fileName != "")
38. {
39. if (file.ContentLength >= 1024 * 1024 * 2)
40. {
41. infos = "上传文件太大,目前仅支持2M以内的图片上传!";
42. }
43. else
44. {
45. fileExtension = System.IO.Path.GetExtension(fileName).ToLower();
46. String[] allowedExtensions = { ".jpg", ".jpeg", ".gif", ".bmp", ".png", ".icon" };
47. for (int i = 0; i < allowedExtensions.Length; i++)
48. {
49. if (fileExtension == allowedExtensions[i])
50. {
51. fileOK = true;
52. break;
53. }
54. }
55. if (!fileOK)
56. {
57. infos = "不支持上传此类型文件!目前支持的图片格式有:jpg|jpeg|gif|bmp|png|icon";
58. }
59. else
60. {
61. file.SaveAs(System.Web.HttpContext.Current.Request.MapPath("~/books/BookPic/") + theFileName);
62. infos = "ok" + theFileName;
63. }
64. }
65. }
66. else
67. {
68. infos = "没有读取到文件!";
69. }
70. return infos;
71. }
72.}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/duanbukui/archive/2009/06/14/4265575.aspx