با سلام
می خواستم بدانم نحوه آپلود فایل در vb.net چگونه است.
من با استفاده از opendialog آدرس فایل را در یک textbox بر می گردانم و حالا می خواهم وقتی کاربر یک دکمه را زد آن فایل به پوشه مورد نظر من انتقال پیدا کند.
Printable View
با سلام
می خواستم بدانم نحوه آپلود فایل در vb.net چگونه است.
من با استفاده از opendialog آدرس فایل را در یک textbox بر می گردانم و حالا می خواهم وقتی کاربر یک دکمه را زد آن فایل به پوشه مورد نظر من انتقال پیدا کند.
سلام
نام فایل رو که از opendialog گرفتی با دستور filecopy به شاخه مورد نظر انتقال بده
سلام دوست عزیز
مثالی ساده از MSDN :
My.Computer.Network.UploadFile ("C:\My Documents\Order.txt", _
"http://www.cohowinery.com/upload.aspx","","",True,500)
به گمونم منظور شما تو اینترنتِ . اگه منظورت تو اینترنتِ باید با ASP.net کار کنی . اگه خواستی بگو کدش رو برات بذارم
نه در اینترنت نیست.
من کد زیر را استفاده کردم ولی خطا می دهد:
My.Computer.FileSystem.CopyFile(TextBox1.Text, "D:\Aqua")
سلام
دوست عزیز باید در مبدا و مقصد مسیر کامل همراه با نام فایل نوشته شود.
دوست عزیز خنگی منو ببخشید ولی هر کاری کردم نشد.
می شود یک کد کامل از این قسمت به من بدهید؟
سلام
دوست عزیز از آنچه فکر میکنی ساده تره . به هر حال من یک مثال برات نوشتم .
به یاد داشته باش که کنترلها را انجام نداده ام
فایلی را که میخوای کپی کنی انتخاب میکنی ( filebrowser ) یا اینکه دستی نام و مسیر فایل را در تکست اول مینویسی.
مسیری را که میخوای فایل را در آن کپی کنی در تکست 2 مینویسی البته میتوانی با استفاده از folderbrowser هم شاخه مورد نظر را انتخاب کنی .
با کلیک روی copy فایل به مسیر مورد نظر ( البته با همان نام ) کپی میشود.
دوست عزیز در کد شما خطای زیر است:
من فکر می کنم شما منظور من را متوجه نشدید.
به طور مثال من می خواهم عکسa را از یک پوشه بردارم و فقط در پوشه b بریزم.
سلام
دوست عزیز این کد برای من خطا نمیده . تابع مورد اشاره برای برگرداندن نام فایل هستش . شما از کدام نسخه vs استفاده میکنید ؟ مال من 2005 میباشد.
درستش کردم.
از راهنمایتون ممنون.
دوست عزیز توجه کنید :
از تابع FileCopy برای آپلود استفاده نمی شود ،
شما میبایست از متد UploadFile مربوط به کلاس My.Computer.Network.UploadFile استفاده نمایید ،
و یا خوتان با استفاده کلاسهای فضای نام System.Net از جمله FtpWebRequest و WebRequest اینکار را انجام دهید.
دوست عزیز مواردی که ذکر شد تنها مختص WebApplication نیست ، /نقل قول:
سلام
ایشان با win application کار میکنند نه با web application یعنی با vb.net نه asp.net
یک نمونه از FTP Client:
http://www.freevbcode.com/source/TransData.zip
و مثالی از MSDN (سی شارپ):
using System;
using System.Net;
using System.Threading;
using System.IO;
namespace Examples.System.Net
{
public class FtpState
{
private ManualResetEvent wait;
private FtpWebRequest request;
private string fileName;
private Exception operationException = null;
string status;
public FtpState()
{
wait = new ManualResetEvent(false);
}
public ManualResetEvent OperationComplete
{
get {return wait;}
}
public FtpWebRequest Request
{
get {return request;}
set {request = value;}
}
public string FileName
{
get {return fileName;}
set {fileName = value;}
}
public Exception OperationException
{
get {return operationException;}
set {operationException = value;}
}
public string StatusDescription
{
get {return status;}
set {status = value;}
}
}
public class AsynchronousFtpUpLoader
{
// Command line arguments are two strings:
// 1. The url that is the name of the file being uploaded to the server.
// 2. The name of the file on the local machine.
//
public static void Main(string[] args)
{
// Create a Uri instance with the specified URI string.
// If the URI is not correctly formed, the Uri constructor
// will throw an exception.
ManualResetEvent waitObject;
Uri target = new Uri (args[0]);
string fileName = args[1];
FtpState state = new FtpState();
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(target);
request.Method = WebRequestMethods.Ftp.UploadFile;
// This example uses anonymous logon.
// The request is anonymous by default; the credential does not have to be specified.
// The example specifies the credential only to
// control how actions are logged on the server.
request.Credentials = new NetworkCredential ("anonymous","janeDoe@contoso.com");
// Store the request in the object that we pass into the
// asynchronous operations.
state.Request = request;
state.FileName = fileName;
// Get the event to wait on.
waitObject = state.OperationComplete;
// Asynchronously get the stream for the file contents.
request.BeginGetRequestStream(
new AsyncCallback (EndGetStreamCallback),
state
);
// Block the current thread until all operations are complete.
waitObject.WaitOne();
// The operations either completed or threw an exception.
if (state.OperationException != null)
{
throw state.OperationException;
}
else
{
Console.WriteLine("The operation completed - {0}", state.StatusDescription);
}
}
private static void EndGetStreamCallback(IAsyncResult ar)
{
FtpState state = (FtpState) ar.AsyncState;
Stream requestStream = null;
// End the asynchronous call to get the request stream.
try
{
requestStream = state.Request.EndGetRequestStream(ar);
// Copy the file contents to the request stream.
const int bufferLength = 2048;
byte[] buffer = new byte[bufferLength];
int count = 0;
int readBytes = 0;
FileStream stream = File.OpenRead(state.FileName);
do
{
readBytes = stream.Read(buffer, 0, bufferLength);
requestStream.Write(buffer, 0, readBytes);
count += readBytes;
}
while (readBytes != 0);
Console.WriteLine ("Writing {0} bytes to the stream.", count);
// IMPORTANT: Close the request stream before sending the request.
requestStream.Close();
// Asynchronously get the response to the upload request.
state.Request.BeginGetResponse(
new AsyncCallback (EndGetResponseCallback),
state
);
}
// Return exceptions to the main application thread.
catch (Exception e)
{
Console.WriteLine("Could not get the request stream.");
state.OperationException = e;
state.OperationComplete.Set();
return;
}
}
// The EndGetResponseCallback method
// completes a call to BeginGetResponse.
private static void EndGetResponseCallback(IAsyncResult ar)
{
FtpState state = (FtpState) ar.AsyncState;
FtpWebResponse response = null;
try
{
response = (FtpWebResponse) state.Request.EndGetResponse(ar);
response.Close();
state.StatusDescription = response.StatusDescription;
// Signal the main application thread that
// the operation is complete.
state.OperationComplete.Set();
}
// Return exceptions to the main application thread.
catch (Exception e)
{
Console.WriteLine ("Error getting response.");
state.OperationException = e;
state.OperationComplete.Set();
}
}
}
}
در ضمن مطالب زیر را نیز مطالعه کنید :
http://msdn2.microsoft.com/en-us/lib...ebrequest.aspx
http://msdn2.microsoft.com/en-us/lib...bresponse.aspx
من یه هاست دارم آیا می شه یک فایل رو از سایت دیگه به هاست خودم کپی کنم؟
سلام این چه صفحه ای باید باشه؟
http://www.cohowinery.com/upload.aspx