当前位置: 代码迷 >> ASP.NET >> asp.net 2.0 FileUpload 上传图片解决办法
  详细解决方案

asp.net 2.0 FileUpload 上传图片解决办法

热度:2020   发布时间:2013-02-26 00:00:00.0
asp.net 2.0 FileUpload 上传图片
哪位仁兄能发个例子给我谢谢   。。。。
zxd0035@163.com

------解决方案--------------------------------------------------------
下面的代码示例演示如何创建 FileUpload 控件,该控件将文件保存到代码中指定的路径。调用 SaveAs 方法将文件保存到服务器上的指定路径。

Visual Basic 复制代码
<%@ Page Language= "VB " %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<script runat= "server ">

Sub UploadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)

' Specify the path on the server to
' save the uploaded file to.
Dim savePath As String = "c:\temp\uploads\ "

' Before attempting to perform operations
' on the file, verify that the FileUpload
' control contains a file.
If (FileUpload1.HasFile) Then
' Get the name of the file to upload.
Dim fileName As String = FileUpload1.FileName

' Append the name of the file to upload to the path.
savePath += fileName

' Call the SaveAs method to save the
' uploaded file to the specified path.
' This example does not perform all
' the necessary error checking.
' If a file with the same name
' already exists in the specified path,
' the uploaded file overwrites it.
FileUpload1.SaveAs(savePath)

' Notify the user of the name the file
' was saved under.
UploadStatusLabel.Text = "Your file was saved as " & fileName

Else
' Notify the user that a file was not uploaded.
UploadStatusLabel.Text = "You did not specify a file to upload. "
End If

End Sub

</script>

<html xmlns= "http://www.w3.org/1999/xhtml " >
<head runat= "server ">
<title> FileUpload Example </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<h4> Select a file to upload: </h4>

<asp:FileUpload id= "FileUpload1 "
runat= "server ">
</asp:FileUpload>

<br /> <br />

<asp:Button id= "UploadButton "
Text= "Upload file "
OnClick= "UploadButton_Click "
runat= "server ">
</asp:Button>

<hr />

<asp:Label id= "UploadStatusLabel "
runat= "server ">
</asp:Label>
</div>
</form>
</body>
</html>


C# 复制代码
<%@ Page Language= "C# " %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN " "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

<script runat= "server ">

protected void UploadButton_Click(object sender, EventArgs e)
{
// Specify the path on the server to
// save the uploaded file to.
String savePath = @ "c:\temp\uploads\ ";

// Before attempting to perform operations
  相关解决方案