Asp.net 图像上传到文件系统
如何将图像上载到文件系统 使用 例如(根据链接的MSDN文章修改),如果您只需要一个简单的表单将文件上载到服务器上的路径,可以从以下内容开始:Asp.net 图像上传到文件系统,asp.net,Asp.net,如何将图像上载到文件系统 使用 例如(根据链接的MSDN文章修改),如果您只需要一个简单的表单将文件上载到服务器上的路径,可以从以下内容开始: <%@ Page Language="C#" %> <script runat="server"> protected void UploadFileAction_Click(object sender, EventArgs e) { var fileStoragePath = Server.Ma
<%@ Page Language="C#" %>
<script runat="server">
protected void UploadFileAction_Click(object sender, EventArgs e)
{
var fileStoragePath = Server.MapPath("~/Uploads");
if (fileUploader.HasFile)
{
fileUploader.SaveAs(Path.Combine(fileStoragePath, fileUploader.FileName));
outputLabel.Text = string.Format(
"File Name: {0}<br />File Size: {1}kb<br />Content Type: {2}",
fileUploader.PostedFile.FileName,
fileUploader.PostedFile.ContentLength,
fileUploader.PostedFile.ContentType
);
}
else
outputLabel.Text = "You have not specified a file.";
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Upload A File</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:FileUpload ID="fileUploader" runat="server" /><br />
<br />
<asp:Button ID="uploadFileAction" runat="server" OnClick="UploadFileAction_Click" Text="Upload File" /> <br />
<br />
<asp:Label ID="outputLabel" runat="server"></asp:Label></div>
</form>
</body>
</html>
受保护的无效上载文件操作\u单击(对象发送方,事件参数e)
{
var fileStoragePath=Server.MapPath(“~/Uploads”);
if(fileUploader.HasFile)
{
fileUploader.SaveAs(Path.Combine(fileStoragePath,fileUploader.FileName));
outputLabel.Text=string.Format(
“文件名:{0}
文件大小:{1}kb
内容类型:{2}”,
fileUploader.PostedFile.FileName,
fileUploader.PostedFile.ContentLength,
fileUploader.PostedFile.ContentType
);
}
其他的
outputLabel.Text=“您尚未指定文件。”;
}
上传文件
以及(指更多关于其他问题的答案,这是一个热门话题)。和往常一样,服务器端验证是必要的,即使对于良好的UX仍然建议使用客户端验证。永远不要隐式信任客户端验证,也要始终验证服务器上的用户输入。以下是代码示例:
您的html:
<input type="file" name="Pic_0001">
虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,只有链接的答案可能会无效。@Rob:谢谢你提醒我这一点。这是我以前的“肇事逃逸”答案之一,我从未回去更新过。修正。综合答案+1!=)
'this is your file name at html page
Dim HtmlFilename As String = "Pic_0001"
'the place to manipulate all uploaded files
Dim collection As System.Web.HttpFileCollection
collection = Page.Request.Files
'for example, you have selected a picture file named hotdog.jpg in browser
'this variable will manipulate your hotdog.jpg file
Dim UploadedFile As System.Web.HttpPostedFile
'retrieve the reference to your file
UploadedFile = collection.Item(HtmlFilename)
'this is the location to save your uploaded file
Dim WhereToSave As String = "c:\test folder\hotdog.jpg"
'this is your folder that will contain the uploaded file
Dim Folderpath As String = System.IO.Path.GetDirectoryName(WhereToSave)
'now do checking if the folder exists, if not create the folder
'NOTE: this step is needed to prevent folder not exists error
If System.IO.Directory.Exists(Folderpath) = False Then
System.IO.Directory.CreateDirectory(Folderpath)
End If
'now actually save your file to the server
UploadedFile.SaveAs(WhereToSave)