これはApp_Codeフォルダーにあります。ファイルをアップロードするためのカスタムコントロールを作成しています。AllowMultipleプロパティをFileUploadコントロールに追加する必要があります。どうすればよいですか?ファイルのアップロードがどこにあるかを確認するには、コード内のコメントを参照してください。msdnWebサイトからそれを行う方法を理解できません。
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();
}
}
.NET Framework 4.5以降を使用している場合、そのプロパティは既に存在します。
これはHTML5に依存していると私は信じていることに注意してください。
そうでない場合は、Attributesプロパティを使用して、ページにレンダリングされるHTML属性を追加できます。
browser.Attributes["multiple"] = "multiple"
これは、WebサイトがHTML5を使用している場合にのみ機能します。
FileUploadから新しいクラスを派生させ、そのクラスにプロパティを追加してから、AddAttributesToRender(または他のrenderメソッドの1つ)メソッドをオーバーライドして、適切なHTMLを出力することもできます。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加