PDA

View Full Version : کد آپلود عکس در هاست



saeid taheri
جمعه 08 تیر 1386, 14:04 عصر
سلام من خیلی گشتم ولی تاپیک مورد نظرم که بتونه کمک کنه پیدا نکردم

میشه کدی برای آپلود عکس توسط اپراتور برام بزارید

ممنون

ClaimAlireza
جمعه 08 تیر 1386, 14:12 عصر
html:



<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="picture:"></asp:Label>
<asp:FileUpload ID="FileUpload1" runat="server" /><br />
<br />
&nbsp; &nbsp;
<asp:Label ID="Label2" runat="server" Text="note:"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Width="229px"></asp:TextBox><br />
<br />
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ManualInsertConnectionString %>"
InsertCommand="INSERT INTO Table_1(pictureUrl, note, path) VALUES (@pictureUrl, @note, @path)"
SelectCommand="SELECT Table_1.* FROM Table_1">
<InsertParameters>
<asp:ControlParameter Name="pictureUrl" ControlID="FileUpload1" PropertyName="FileName" Type="String" />
<asp:ControlParameter Name="note" ControlID="TextBox1" PropertyName="Text" Type="String"/>
<asp:ControlParameter Name="path" ControlID="combine" PropertyName="Text" Type="string" />
</InsertParameters>
</asp:SqlDataSource>
<br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Upload" />&nbsp;<br />
<br />
&nbsp;&nbsp;
</div>
</form>
</body>
</html>


C#



using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
string saveFile;
string savePath;
saveFile = Path.Combine(Request.PhysicalApplicationPath, "UploadDirectory");
savePath = Path.Combine(saveFile, FileUpload1.FileName);
FileUpload1.SaveAs(savePath);
combine.Text = Path.Combine("UploadDirectory", FileUpload1.FileName);
SqlDataSource1.Insert();
}
}

babi_wd
جمعه 08 تیر 1386, 14:17 عصر
چندین بار بحث شده اما چون کد ها تو MSDN دمه دسته برات میزارم


<%@ 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
// on the file, verify that the FileUpload
// control contains a file.
if (FileUpload1.HasFile)
{
// Get the name of the file to upload.
String fileName = 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 of 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.";
}

}
</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>