Ahoj, minulý mesiac som kúpil hosting
asphostportal.com. A teraz, mám malý problém s mojím projektom. Dúfam, že mi môžeš pomôcť. Mám to v priečinku App_Code. Vytváram vlastný ovládací prvok pre upload súborov. Musím sa pridať AllowMultiple vlastnosť FileUpload kontrole. Ako to mám urobiť? Pozri poznámky v kóde vidieť kde fileupload, nemôžete vymyslieť, ako to urobiť z webovej lokalite msdn.
Kód:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Drawing;
using System.Security.Permissions;
/// <summary>
/// Summary description for MultiFileUpload
/// </summary>
using System.IO;
using System.Configuration;
using System.Drawing.Imaging;
namespace MyControls
{
[ToolboxData("<{0}:MultiFileUpload runat=server></{0}:MultiFileUpload>")]
public class MultiFileUpload : CompositeControl
{
public string tempFolderPath;
private FileUpload browser;
private ListBox fileList;
private Button addToListButton;
private Button delFromListButton;
private Button uploadFiles;
private string uploadPath;
public string thumbsPath;
// [BrowsableAttribute(true)]
// public virtual bool AllowMultiple { get; set AllowMultiple=true; }
protected override void CreateChildControls()
{
// need to set AllowMultiple=true on here for the fileupload. If there is a way aspx page that would work too
browser = new FileUpload();
fileList = new ListBox();
addToListButton = new Button();
delFromListButton = new Button();
uploadFiles = new Button();
browser.Width = new Unit(350);
fileList.Width = new Unit(265);
addToListButton.Width = new Unit(75);
delFromListButton.Width = new Unit(75);
uploadFiles.Width = new Unit(353);
addToListButton.Text = "Add";
delFromListButton.Text = "Delete";
uploadFiles.Text = "Upload to Site";
addToListButton.Click += new EventHandler(AddToListButtonClick);
delFromListButton.Click += new EventHandler(DelFromListButtonClick);
uploadFiles.Click += new EventHandler(UploadFilesClick);
this.Controls.Add(new LiteralControl("<table><tr><td colspan='2'>"));
this.Controls.Add(browser);
this.Controls.Add(new LiteralControl("<td></tr><tr><td rowspan='2' width='20'>"));
this.Controls.Add(fileList);
this.Controls.Add(new LiteralControl("</td><td>"));
this.Controls.Add(addToListButton);
this.Controls.Add(new LiteralControl("</td></tr><tr><td colspan='2'>"));
this.Controls.Add(delFromListButton);
this.Controls.Add(new LiteralControl("</td></tr><table>"));
this.Controls.Add(uploadFiles);
base.CreateChildControls();
}
protected override void Render(HtmlTextWriter writer) {base.Render(writer);}
public MultiFileUpload() {}
public void SetUploadPath(string path) {this.uploadPath = path;}
public string GetUploadPath() {return this.uploadPath;}
private void AddToListButtonClick(object source, EventArgs e)
{
if (browser.HasFile) {
DirectoryInfo tempFolder = new DirectoryInfo(tempFolderPath);
if (tempFolder.Exists)
{
browser.SaveAs(tempFolderPath + browser.FileName);
}
}
RefreshListBox();
}
private void DelFromListButtonClick(object source, EventArgs e)
{
if (fileList.SelectedIndex != -1)
{
DirectoryInfo tempFolder = new DirectoryInfo(tempFolderPath);
tempFolder.GetFiles().ElementAt(fileList.SelectedIndex).Delete();
RefreshListBox();
}
}