【发布时间】:2014-03-19 12:16:05
【问题描述】:
我想使用 ASP.NET 执行一个简单的操作。我希望用户选择尽可能多的图像并将这些图像保存到服务器上运行的数据库中。每次用户想要添加新图像时,他单击“添加”以添加新图像。图像具有附带的属性,例如拍摄位置和时间。位置通过文本框输入,日期通过日期选择器输入。
问题在于将图像保存到服务器。
是否有任何控件可以执行此任务?
我曾尝试使用 Upload 控件和 Devexpress ASPxUpload 控件,但无济于事。上传控件还要求我先将图像上传到服务器,然后再将其保存到数据库。
我尝试将代表图像及其日期和位置的控件实现为用户控件,但是每当我单击上传按钮时,都不会调用该事件
这是用户控件的代码
public partial class ucVerificationItem : System.Web.UI.UserControl
{
public byte[] data { set; get; }
public byte[] Photo { get { return data; } }
//ASPxFormLayout1_E3.Text is a Text Box which has the verification text
public string Verify { get { return ASPxFormLayout1_E3.Text; } }
//ASPxFormLayout1_E4.Text is a Text Box which has the caption text
public string Caption { get { return ASPxFormLayout1_E4.Text; } }
//ASPxFormLayout1_E5.Date is a DateEdit which has the date
public DateTime Date { get { return ASPxFormLayout1_E5.Date; } }
// to expose the button to the page from which it is gonna be called
public ASPxButton Button { get { return ASPxButton1; } }
//to expose the uplaod control
public ASPxUploadControl UploadControl { get { return ASPxFormLayout1_E2; } }
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ASPxButton1_Click(object sender, EventArgs e)
{
ASPxFormLayout1_E2.UploadedFiles[0].SaveAs(Server.MapPath("~\\temp\\"+ASPxFormLayout1_E2.UploadedFiles[0].FileName));
}
protected void ASPxFormLayout1_E2_FileUploadComplete(object sender, DevExpress.Web.ASPxUploadControl.FileUploadCompleteEventArgs e)
{
ASPxFormLayout1_E2.UploadedFiles[0].PostedFile.InputStream.Read(data, 0, ASPxFormLayout1_E2.PostedFile.ContentLength);
}
}
这是调用控件的页面代码
protected void Page_Init(object sender, EventArgs e) {
if (IsPostBack)
{
if (Session["Photos"] == null)
{
List<ucVerificationItem> list = new List<ucVerificationItem>();
Session["Photos"] = list;
}
foreach (ucVerificationItem item in Session["Photos"] as List<ucVerificationItem>)
{
PlaceHolder1.Controls.Add(item);
}
}
}
protected void addPhotoASPxButton_Click(object sender, EventArgs e)
{
ucVerificationItem item = (ucVerificationItem)Page.LoadControl("~/Company/Forms/ucVerificationItem.ascx");
(Session["Photos"] as List<ucVerificationItem>).Add(item);
PlaceHolder1.Controls.Add(item);
}
希望这足够清楚
【问题讨论】: