Upload Images Help
Now I have a question:
TestUploadImage.aspx
<%@ Page language="c#" Codebehind="TestUploadImage.aspx.cs" AutoEventWireup="false" Inherits="OVOP.Admin.TestUploadImage" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>TestUploadImage</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script language="Javascript">
function GetImage(CtrlName)
{
/************************************************** ** Use Javascript method (window.open) to PopUp a new window which contain a Calendar Control. In the meantime, we'll pass the Parent Form Name and Request Control Name in the QueryString! ************************************************** ***/
ChildWindow = window.open('UploadImages.aspx?FormName=' + document.forms[0].name + '&CtrlName=' + CtrlName, "UploadImage", "width=350,height=300,top=200,left=200,toolbars=no ,scrollbars=no,status=no,resizable=no"); } function CheckWindow() { ChildWindow.close(); }
</script>
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:textbox id="txtImage" runat="server" Width="200px"></asp:textbox><A href="javascript:GetImage('txtImage')"><IMG alt="Image Upload" src="SmallCalendar.gif" border="0"></A>
</form>
</body>
</HTML>
UploadImages.aspx
using System;
using System.Configuration;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.IO;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace OVOP.Admin
{
/// <summary>
/// Summary description for UploadImages.
/// </summary>
public class UploadImages : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlInputButton btnCloseWindow;
protected System.Web.UI.HtmlControls.HtmlInputButton btnUploadFile;
protected System.Web.UI.HtmlControls.HtmlInputFile UploadFile;
public string uploadFolder="D:\\Multi Ltd Projects\\OVOP\\ProductImages\\";
public string strFormName;
public string strCtrlName;
protected System.Web.UI.WebControls.Button Button1;
public string strSelectedImage;
private void Page_Load(object sender, System.EventArgs e)
{
if (!IsPostBack)
{
if (UploadFile.PostedFile != null)
{
try
{
// Put user code to initialize the page here
string sPath=uploadFolder;
//Split the client side path (C:\Picture Images\images.jpg) by taken '\' as a delimiter.
//Take the last index of the array return by splitting.
string clientsideFilePath = UploadFile.PostedFile.FileName;
char[] delimeter = "\\".ToCharArray();
string[] strList = clientsideFilePath.Split(delimeter, 100);
string fileNameOnly = strList[strList.Length-1];
Stream imgStream = UploadFile.PostedFile.InputStream;
int imgLen = UploadFile.PostedFile.ContentLength;
//UploadFile.PostedFile.SaveAs("C:\\Temp\\"+UploadFi le.Value);
string imgContentType = UploadFile.PostedFile.ContentType;
//strSelectedImage = fileNameOnly;
//txtImgName.Value = fileNameOnly;
byte[] imgBinaryData = new byte[imgLen];
int n = imgStream.Read(imgBinaryData,0,imgLen);
strSelectedImage = fileNameOnly;
UploadFile.PostedFile.SaveAs(sPath + fileNameOnly);
Response.Write("Insert Sucessfully");
}
catch(Exception exc)
{
Response.Write("Exception in Main: " + exc.ToString());
}
}
}
strFormName = Request.QueryString["FormName"];
strCtrlName = Request.QueryString["CtrlName"];
//strSelectedImage = fileNameOnly;
//strSelectedImage=UploadFile.PostedFile.FileName;
// strSelectedImage = fileNameOnly;
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
if (UploadFile.PostedFile != null)
{
try
{
// Put user code to initialize the page here
string sPath=uploadFolder;
//Split the client side path (C:\Picture Images\images.jpg) by taken '\' as a delimiter.
//Take the last index of the array return by splitting.
string clientsideFilePath = UploadFile.PostedFile.FileName;
char[] delimeter = "\\".ToCharArray();
string[] strList = clientsideFilePath.Split(delimeter, 100);
string fileNameOnly = strList[strList.Length-1];
Stream imgStream = UploadFile.PostedFile.InputStream;
int imgLen = UploadFile.PostedFile.ContentLength;
//UploadFile.PostedFile.SaveAs("C:\\Temp\\"+UploadFi le.Value);
string imgContentType = UploadFile.PostedFile.ContentType;
//strSelectedImage = fileNameOnly;
//txtImgName.Value = fileNameOnly;
byte[] imgBinaryData = new byte[imgLen];
int n = imgStream.Read(imgBinaryData,0,imgLen);
UploadFile.PostedFile.SaveAs(sPath + fileNameOnly);
strSelectedImage = fileNameOnly;
Response.Write(fileNameOnly);
Response.Write("Insert Sucessfully");
}
catch(Exception exc)
{
Response.Write("Exception in Main: " + exc.ToString());
}
}
}
}
}
when I select a image name it doesn't effect into textbox, that means when I press select file button it will get the image file name into textbox
Thanks
Lee
|