PDA

View Full Version : upload عکس



oranoos
چهارشنبه 12 اسفند 1383, 16:51 عصر
سلام
من می خواهم در برنامه ای که با vb.net نوشته شده این امکان را به کاربر بدهم که بتواند عکس ارسال کند ،لطفا در این زمینه مرا راهنمایی نمایید(خصوصا در مورد کد مورد نیاز این برنامه)

kochol
چهارشنبه 12 اسفند 1383, 17:32 عصر
سلام

اگه تو صفحات asp.net می خواهی این کارو بکنی بیا یه کد خوب.


Getting Files from the Client
Use the File Field HTML control to upload files from the client to the server. The File Field HTML control is actually a Text Field HTML control and a Submit Button HTML control bound together. Clicking the Browse button runs a built-in script that displays the Windows Choose File dialog box on the client’s computer, as shown in Figure 4-15.


Figure 4-15. The Windows Choose File dialog box
To receive the selected client file on the server, follow these steps:

Draw a File Field HTML control on a Web form.

Right-click the control, and select Run As Server Control from the shortcut menu.

Right-click the Web form, and select View HTML Source. Visual Studio displays the HTML code for the Web form.

Add an enctype attribute to the <form> tag in the Web form’s HTML. The enctype attribute sets the MIME type of the form, and uploading a file requires both a MIME type of multipart/form-data and a form method of post, as shown here in boldface:

<form action="webform1.aspx" method="post" enctype="multipart/form-data" runat="server" ID="Form1">
Right-click the Web form, and select View Design from the shortcut menu. Visual Studio displays the Web Forms Designer.

Add a Button control and a Click event procedure to the Web form to get and save the selected file. For example, the following event procedure saves the file to the server:

Visual Basic .NET
Private Sub butUpload_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles butUpload.Click
Dim strFilename As String
Try
' Get the file name.
strFilename = filUpload.PostedFile.FileName
' Exit if no file name was entered.
If strFilename = "" Then Return
' If file has zero length (did not exist on client)
If filUpload.PostedFile.ContentLength = 0 Then
' Display message.
lblMsg.Text = "File not found on client or contains no data."
' Exit.
Return
End If
' Get the base name for the file (exclude path).
strFilename = System.IO.Path.GetFileName(strFilename)
' Save uploaded file to server.
filUpload.PostedFile.SaveAs(Request.MapPath("./uploadfiles") & _
"\" & strFilename)
' Add file to list of server files.
lstServerFiles.Items.Add(strFilename)
' Select an item in the list.
lstServerFiles.SelectedIndex = 0
Catch ex As System.UnauthorizedAccessException
lblMsg.Text = "You must set the permissions on the server " & _
"to allow the ASPNET user to write to the destination folder."
Catch ex As Exception
lblMsg.Text = "An unexpected error occurred: " & ex.Message
Finally
End Try
End Sub
Visual C#
private void butUpload_Click(object sender, System.EventArgs e)
{
string strFilename;
try
{
// Get the file name.
strFilename = filUpload.PostedFile.FileName;
// Exit if no file name was entered.
if (strFilename == "") return;
// if file is zero length, it is empty or doesn't exist.
if (filUpload.PostedFile.ContentLength == 0)
{
lblMsg.Text = "File was not found on client
or file contains no data.";
return;
}
// Get the base name for the file (exclude path).
strFilename = System.IO.Path.GetFileName(strFilename);
// Save uploaded file to server.
filUpload.PostedFile.SaveAs(Request.MapPath("./uploadfiles")
+ "\\" + strFilename);
// Add file to list of server files.
lstServerFiles.Items.Add(strFilename);
// Select an item in the list.
lstServerFiles.SelectedIndex = 0;
}
catch (System.UnauthorizedAccessException ex)
{
lblMsg.Text = "You must set the permissions on the server
to allow the ASPNET user to write to the destination folder.";
}
catch (Exception ex)
{
lblMsg.Text = "An unexpected error occurred: "
+ ex.Message;
}
finally {}
}
The PostedFile method of the File Field HTML control represents the file being uploaded. You can use the FileName property and SaveAs method of the returned object to save the file on the server, as shown in the preceding code. The following code shows one way to retrieve the file from the server. In this case, the file is displayed in the browser.

Visual Basic .NET
Private Sub butView_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles butView.Click
Dim strFilename As String
' Get selected file name.
strFilename = lstServerFiles.SelectedItem.ToString
' Display as Web page.
Response.Redirect("./uploadfiles/" & "/" & strFilename)
End Sub
Visual C#
private void butView_Click(object sender, System.EventArgs e)
{
// Get selected file name.
string strFilename =
lstServerFiles.SelectedItem.ToString();
// Display as web page.
Request.MapPath(".\\uploadfiles\\") + strFilename);
}

arshia_
چهارشنبه 12 اسفند 1383, 17:35 عصر
سلام
اگر دنبال برنامه ای هستید که با اون بشه عکس رو روی سرور ذخیره کرد ...من هم دنبالش هستم..متاسفانه نمی دونم چرا اجازه نوشتن نمی ده...با کمک دوستان خیلی کارها کردم حتی اجازه نوشتن رو هم به فولدر مورد نظرم دادم اما بازم نمی شه...مسکل اینه که آدرس اینترنتی رو قبول نمی کنه و انتظار آدرس واقعی(فیزیکی ) داره... مثلا وقتی بخوام اونو روی آدرس C:\test ذخیره کنم کار می کنه اما اگر بخوام به صورت مثلا \folder\test آدرس بدم مشکل ساز می شه..اگر بتونم راه درستش رو پیدا کنم حتما برای شما هم ارسال می کنم...
یه چیز دیگه که خیلی لازم دارم اینه که بتونم یه نمودار پیشرفت آپلود فایل رو ایجاد کنم تا کاربر بتونه بفهمه چقدر از فایلش آپلود شده و چقدرش باقی مونده...اما خب گکون کنم این یکی خیلی پیچیده تره وووپس فعلا رو اولی کار می کنم ....
:wink:

kochol
چهارشنبه 12 اسفند 1383, 18:45 عصر
سلام

شما می تونید با server.mappath مکان برنامه تون را روی سرور پیدا کنید و فایلتونو ذخیره کنید.

ehsan707
جمعه 14 اسفند 1383, 19:54 عصر
البته باید قبل از او مجوز نوشتن به دایرکتوری مربوط رو بدید.

اگه نیاز باشه که فایل رو با ویروس اسکنر برسی کنیم چی؟ کسی این کار رو کرده

oranoos
شنبه 15 اسفند 1383, 11:47 صبح
سلام
از پاسخی که ارسال نموده اید متشکرم، اگر ممکنه راجع به lstServerFiles توضیح بیشتری بدهید.

oranoos
یک شنبه 16 اسفند 1383, 08:40 صبح
سلام
زمانی که برنامه اجرا می شه ، خطا میده(Access to the path "c:\inetpub\wwwroot\imagsave2\upload\018.jpg" is denied.)
کسی می دونه چطور باید اجازه دستیابی برای این فایل تعریف کرد ؟

arshia_
یک شنبه 16 اسفند 1383, 09:00 صبح
من هم یه مشکل که دارم اینه
بهتره در موردش توضیح بدم
وقتی که با استفاده از این کد می خوام فایلی رو توی مثلا C;\test ذخیره کنم برنامه به راحتی کار می کنه ...حالا اگر بخوام همین کد فایل رو توی مثلا D:\Inetpub\wwwroot\Test ذخیره کنه خطای عدم دسترسی می ده یعنی همین مشابه خطایی که دوستمون نوشته...
من تنظیمات فولدر رو بررسی کردم حتی توی IIS هم اجازه read , write , ... همه رو دادم اما باز هم نتونستم فایل رو ذخیره کنم... :flower:
توی اینترنت هم دقیقا همین مشکل رو دارم...
خلاصه نمی دونم کجای کار مشکل داره که این خطا رو می گیرم...
با روشهای مختلف آدرس دادم اما نشد..مثلا با map یا آدرس کامل مثل C:\net\test یا در نظر گرفتن فولدر جاری یعنی فقط اسم فایل مثل test.jpg اما باز هم نشد...
حالا علت چیه ...نمی دونم....هر چی MSDN می خونم یا منابعی که دوستان گفتن کمکی نمی کنه...حتی استاد عزیز آقای نصیری هم خیلی زحمت کشیدن اما نمی دونم این مشکل کوچولو چیه که نمی ذاره کار درست بشه.و..می دونم که یه ریزه کاری بیشتر نمی خواد...
خلاصه باز هم ممنون از جواب همه دوستان...

esi022
یک شنبه 16 اسفند 1383, 09:45 صبح
سلام
1 - ارشیا جان 018.jpg میشه ببینی readonly نیست؟ بهم نخندیا ولی پیش میاد
2 - Indexing Service رو هم اگه active هست disable کن برای تست.
4 - http://www.dotnet247.com/247reference/msgs/35/175431.aspx
3 - http://www.dovico.com/KBase/WebHelp/kbase2/server_error_access_to_the_path_is_denied.html

mehdi_394
یک شنبه 16 اسفند 1383, 17:32 عصر
ببینید بچه ها برای اینکه به یک فولدر تویه iis بتونید مجوز نوشتن بدید باید روی فولدر سایتتون راست کلیک کنید و از tab sharing -share this folder رو انتخاب کنید (دقیقا جمله اصلی یادم نیست به هر حال باید اون فولدر رو شیر کنید .مشکل من اینطوری حل شد.


:موفق: :موفق: :موفق:

dena
دوشنبه 17 اسفند 1383, 02:52 صبح
من تا بحال برنامه های زیادی نوشته ام که فایل را روی فضای سایت می فرستد . آخرین بار اتفاقا به من هم اجازه فرستادن عکس نمی داد . روی یک سرور کدها کار می کرد روی یکی نمی کرد . عاقبت مشکل از سوی خود مسئولین میزبان وب برطرف شد و کدها هیچ ایرادی نداشت . بنابراین با میزبان سایتتان تماس بگیرید تا به شما اجازه دسترسی بدهند

arshia_
دوشنبه 17 اسفند 1383, 11:00 صبح
ممنون از جوابهای دوستان
من هم فکر کنم قسمتی از مشکل به دست مسولین فضای سایت حل بشه

در ضمن از بابت لینک های خوبی که دوستون گذاشته ممنون...خیلی کمک کرد..
:flower: :تشویق: