FileUpload
FileUpload Example
Select a file to upload:
@inherits ControlComponent
<p>Select a file to upload:</p>
<asp.FileUpload @ref="this.fileUpload">
</asp.FileUpload>
<br />
<br />
<asp.Button Text="Upload file"
OnClick="this.UploadButton_Click">
</asp.Button>
<br />
<asp.Label @ref="this.uploadStatusLabel">
</asp.Label>
@code {
private FileUpload fileUpload;
private Label uploadStatusLabel;
protected void UploadButton_Click(object sender, EventArgs e)
{
// Before attempting to perform operations
// on the file, verify that the FileUpload
// control contains a file.
if (fileUpload.HasFile)
{
// Get the name of the file to upload.
string fileName = fileUpload.FileName;
// Get the size in bytes of the file to upload.
int fileSize = fileUpload.PostedFile.ContentLength;
// Get the extension of the uploaded file.
string extension = System.IO.Path.GetExtension(fileName);
// Notify the user of the name of the file
// was saved under.
uploadStatusLabel.Text = "Your file was saved as " + fileName
+ " ( fileSize:" + fileSize + " extension:" + extension + " )";
}
else
{
// Notify the user that a file was not uploaded.
uploadStatusLabel.Text = "You did not specify a file to upload.";
}
}
}
FileUpload PostedFileChanged Example
Select an image file:
@inherits ControlComponent
<p>Select an image file:</p>
<asp.FileUpload @ref="this.fileUpload" AutoPostBack="true"
OnPostedFileChanged="this.FileUpload_PostedFileChanged"></asp.FileUpload>
<br />
<br />
<asp.Image @ref="this.image" Visible="false"></asp.Image>
@code {
private FileUpload fileUpload;
private Image image;
protected void FileUpload_PostedFileChanged(object sender, EventArgs e)
{
this.InvokeWaitTask(async () =>
{
if (fileUpload.HasFile)
{
// Save the file to client ObjectURL.
string imageUrl = await fileUpload.SaveAsUrlAsync();
// Show image.
image.ImageUrl = imageUrl;
image.Visible = true;
}
});
}
}
FileUpload Publish Debug On .NET 6, add an InputFile at advance razor.
<InputFile style="visibility: hidden; position: absolute;"></InputFile>
FileUpload Metadata
Name | Type | Kind |
---|---|---|
AccessKey | String | Parameter |
AllowMultiple | Boolean | Parameter |
Attributes | IReadOnlyDictionary | Parameter |
AutoPostBack | Boolean | Parameter |
BackColor | String | Parameter |
BorderColor | String | Parameter |
BorderStyle | BorderStyle | Parameter |
BorderWidth | String | Parameter |
CausesValidation | Boolean | Parameter |
ClientIDMode | ClientIDMode | Parameter |
CssClass | String | Parameter |
Enabled | Boolean | Parameter |
FontBold | Boolean | Parameter |
FontItalic | Boolean | Parameter |
FontNames | String | Parameter |
FontOverline | Boolean | Parameter |
FontSize | String | Parameter |
FontStrikeout | Boolean | Parameter |
FontUnderline | Boolean | Parameter |
ForeColor | String | Parameter |
Height | String | Parameter |
ID | String | Parameter |
Style | String | Parameter |
TabIndex | Int16 | Parameter |
ToolTip | String | Parameter |
ValidationGroup | String | Parameter |
Visible | Boolean | Parameter |
Width | String | Parameter |
OnDataBinding | EventHandler | Event |
OnDisposed | EventHandler | Event |
OnInit | EventHandler | Event |
OnLoad | EventHandler | Event |
OnPostedFileChanged | EventHandler | Event |
OnPreRender | EventHandler | Event |
OnUnload | EventHandler | Event |