Wednesday, January 4, 2017

How to upload files to FTP server using .NET Interop

upload-1118929

In my previous posts i have explained how to download the files from the FTP Server or  move the files from one folder to another on the FTP server. Here are the posts if you have missed them

How to download files from FTP server using .NET Interop

How to move files from one folder to another on FTP server.

In this post i will show you the code i have used to upload the files to the FTP server.   The FTPSetup is a Setup table which I have used to store the path, ftp server, user, and password information.

In the first function (UploadFilesToFTP) I have used an Array to store the paths of all the files from the upload directory, then I iterate through the array and execute second function to upload the file. In the second function it uses FTP Web Request method ‘STOR’ to upload the file to the server.


LOCAL PROCEDURE UploadFilesToFTP@1240060030();
     VAR
       FTPWebRequest@1240060000 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.FtpWebRequest";
       FTPWebResponse@1240060001 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.FtpWebResponse";
       NetworkCredential@1240060002 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.NetworkCredential";
       WebRequestMethods@1240060003 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.WebRequestMethods+File";
       UTF8Encoding@1240060004 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.UTF8Encoding";
       ResponseStream@1240060005 : InStream;
       FileStream@1240060006 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.FileStream";
       TempBlob@1240060008 : TEMPORARY Record 99008535;
       FileName@1240060007 : Text;
       OutStream@1240060009 : OutStream;
       SearchOption@1240060013 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.SearchOption" RUNONCLIENT;
       ArrayHelper@1240060012 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Array" RUNONCLIENT;
       ArrayLength@1240060011 : Integer;
       DirectoryHelper@1240060014 : DotNet "'mscorlib'.System.IO.Directory" RUNONCLIENT;
       i@1240060015 : Integer;
       RelativeServerPath@1240060016 : Text;
       ClientFilePath@1240060017 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.String" RUNONCLIENT;
       PathHelper@1240060010 : DotNet "'mscorlib'.System.IO.Path";
     BEGIN
       GetFTPSetup;
       ArrayHelper := DirectoryHelper.GetFiles(FTPSetup."FTP Local Upload FilePath",'*.txt',SearchOption.TopDirectoryOnly);

      ArrayLength := ArrayHelper.GetLength(0);

      IF ArrayLength = 0 THEN
         EXIT;

      FOR i := 1 TO ArrayLength DO BEGIN
         RelativeServerPath := FORMAT(ArrayHelper.GetValue(i - 1));
          UploadFileToFTP(RelativeServerPath);
       END;
     END;

    LOCAL PROCEDURE UploadFileToFTP@1240060031(FileToUpload@1240060018 : Text);
     VAR
       FTPWebRequest@1240060000 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.FtpWebRequest";
       FTPWebResponse@1240060001 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.FtpWebResponse";
       NetworkCredential@1240060002 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.NetworkCredential";
       WebRequestMethods@1240060003 : DotNet "'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Net.WebRequestMethods+File";
       UTF8Encoding@1240060004 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.Text.UTF8Encoding";
       ResponseStream@1240060005 : InStream;
       FileStream@1240060006 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.FileStream";
      Stream@1240060020 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.Stream";
       FileDotNet@1240060019 : DotNet "'mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.File";
       TempBlob@1240060008 : TEMPORARY Record 99008535;
       FileName@1240060007 : Text;
       OutStream@1240060009 : OutStream;
       SearchOption@1240060013 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.IO.SearchOption" RUNONCLIENT;
        i@1240060015 : Integer;
       RelativeServerPath@1240060016 : Text;
       ClientFilePath@1240060017 : DotNet "'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.System.String" RUNONCLIENT;
       PathHelper@1240060010 : DotNet "'mscorlib'.System.IO.Path";
     BEGIN
       GetFTPSetup;
       FTPWebRequest := FTPWebRequest.Create(FTPSetup."FTP Local Upload FilePath" + PathHelper.GetFileName(FileToUpload));
       FTPWebRequest.Credentials := NetworkCredential.NetworkCredential(FTPSetup."FTP UserName",FTPSetup."FTP Password");
       FTPWebRequest.UseBinary := TRUE;
       FTPWebRequest.UsePassive := TRUE;
       FTPWebRequest.KeepAlive := TRUE;
       FTPWebRequest.Method := 'STOR';

      FileStream := FileDotNet.OpenRead(FileToUpload);
       Stream := FTPWebRequest.GetRequestStream();
       FileStream.CopyTo(Stream);
       Stream.Close;

      FTPWebResponse := FTPWebRequest.GetResponse();
       FTPWebResponse.Close();
     END;

Please leave your comments, feedback or any suggestions you have for me to improve my blog and also if you have any questions, feel free to post.

Share:

74 comments:

Unknown said...

FTP works great only if there is some expert to support it. On the other hand, Binfer can be run on any computer without separate server or client components. See Binfer as FTP alternative

Unknown said...

Thanks for your informative blog!!! Your article helped me to understand the future of .net programming language. Keep on updating your with such awesome information.


dot net training in chennai

madsmorre said...

Hi I read your post and tried to use it, but i'm facing some issues.

The function i'm creating generates a csv-file on the base of an event from another record. The file i'm creation is a tempfile on the server.
I can't seem to find the tempfile when uploading to ftp - I guess that is my problem.

All this should work withot any user involved.

I have determined by test the the function actually creates the file as i'm able to send the appropriate file attached to an email.

How do I make the file available for transferring to remote ftp-server.
Any help will be appriciated.

Regards
Mads Morre

Suresh Kulla said...

Based on how you creating the file please check both on the server and client. What is the issues you are running into ? you are not able to find the file created ? or not able to upload that file to the ftp ?

Thanks
Suresh

mary Brown said...

Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a .Net developer learn from Dot Net Training in Chennai. or learn thru ASP.NET Essential Training Online . Nowadays Dot Net has tons of job opportunities on various vertical industry.
or Javascript Training in Chennai. Nowadays JavaScript has tons of job opportunities on various vertical industry.

Unknown said...

Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
dot-net training in chennai

Anonymous said...

Will this example works with NAV2013 (no R2)?

Suresh Kulla said...

Yes, it will work for NAV 2013.

Mary said...

You have post an extremely valuable data. Furthermore, i have learned heaps of new data by utilizing your blog. Its truly supportive to me. Keep updating. I will take after these tips. Much obliged to you for sharing such point by point article.
B.Com Project Center in Chennai | B.Com Project Center in Velachery

PLC Training Chennai said...

Good Post..Thanks for sharing such a wonderful article.....



PLC Training in Chennai | PLC Training Institute in Chennai | PLC Training Center in Chennai | PLC SCADA Training in Chennai | PLC SCADA DCS Training in Chennai | Best PLC Training in Chennai | Best PLC Training Institute in Chennai | PLC Training Centre in Chennai | Automation Training in Chennai | Automation Training Institute in Chennai

Unknown said...

You have post an extremely valuable data. Thank you..VLSI Projects Center in Chennai | VLSI Projects Center in Velachery

VLSI Training Chennai said...

Your Blog is nice and informative..Thanks for sharing....





VLSI Training in Chennai | Best VLSI Training in Chennai | VLSI Training Centres in Chennai | VLSI Courses in Chennai | VLSI Training Courses in Chennai | VLSI Training Institute in Chennai | VLSI Training Institutes in Chennai | Best VLSI Training Institute in Chennai

Mary said...

The best thing is that your blog really informative thanks for your great information!
B.Com Project Center in Chennai | B.Com Project Center in Velachery

Unknown said...

I really enjoyed with your post..thanks for sharing..
No.1 Software Testing Training Institute in Chennai | Best Selenium Training Institute in Chennai | Java Training in Chennai

Federico Bacile said...
This comment has been removed by the author.
Federico Bacile said...
This comment has been removed by the author.
Joan said...

thanks for your great information. It's very ussefull

blackkutty said...

Much obliged to you a ton for furnishing people with an exceptionally dynamite probability to peruse basic audits from this site.
Latest Updates

sumedhait said...

You have out done yourself this time.
This is probably the best, most concise step by step guide.
Top Institute for VLSI and Embedded in Hyderabad
https://www.sumedhait.com/

alex said...

Have questions about your online casino? Follow the link and find out everything that interests you. best online gambling guide Play with us and win every hour.

jorick228 said...

Try your luck with BGAOC and get your winnings. excellent gambling machines Challenge and win.

Vicky Ram said...

Great post! This is very useful for me and gain more information, Thanks for sharing with us.

easyblogging
Article submission sites

Belago said...

Бытовая и профессиональная светодиодная лента 12в и 24 вольта, я знаю где брать, лучшее на Экодио

service care said...

Hi! Thank you for the share this information. This is very useful information for online blog review readers. Keep it up such a nice posting like this.
oneplus mobile service center in chennai
oneplus mobile service center
oneplus mobile service centre in chennai

Tangki Fiber said...

I feel this is among the such a lot vital info for me. And i am satisfied studying your article. However wanna commentary on few general things, The website style is ideal, the articles is truly nice
Tangki Panel
Tangki Fiberglass
Jual Septic Tank

Kayal said...

Your blog is excellent and very motivated to me. I need more different updates on this topic...
Tableau Training in Chennai
Tableau Course in Chennai
Spark Training in Chennai
Pega Training in Chennai
Excel Training in Chennai
Oracle Training in Chennai
Oracle DBA Training in Chennai
Social Media Marketing Courses in Chennai
Tableau Training in Chennai
Tableau Course in Chennai

shivam said...

Flying Shift - Packers & Movers in Bhopal

easylearn said...

Hi,
Best article, very useful and well explanation. Your post is extremely incredible.Good job & thank you very much for the new information, i learned something new. Very well written. It was sooo good to read and usefull to improve knowledge. Who want to learn this information most helpful. One who wanted to learn this technology IT employees will always suggest you take hadoop certification courses in bangalore.

Vijiaajith said...

good
freeinplanttrainingcourseforECEstudents
internship-in-chennai-for-bsc
inplant-training-for-automobile-engineering-students
freeinplanttrainingfor-ECEstudents-in-chennai
internship-for-cse-students-in-bsnl
application-for-industrial-training

Carlos El Flaco said...

Thank you for your post, What about if I don't know the File Name from the remote Server? I know the Path of the remote server BTW
In Your example the function is expecting a text= FileToupload.
The Line FileDotNet.OpenRead(FileToUpload) won't accept special Char like asterisk
How I can handle this

Venkatesh CS said...

Thanks for sharing valuable information.
Digital Marketing training Course in Chennai
digital marketing training institute in Chennai
digital marketing training in Chennai
digital marketing course in Chennai
digital marketing course training in omr
digital marketing certification in omr
digital marketing course training in velachery
digital marketing training center in Chennai
digital marketing courses with placement in Chennai
digital marketing certification in Chennai
digital marketing institute in Chennai
digital marketing certification course in Chennai
digital marketing course training in Chennai
Digital Marketing course in Chennai with placement
digital marketing courses in Chennai

iuwekjhbr said...

Please refer below if you are looking for best Online job support and proxy interview from India

DevOps Online Job Support From India | PHP Online Job Support From India | Selenium Online Job Support From India | Hadoop Online Job Support From India | Java Online Job Support From India | Angular Online Job Support From India | Python Online Job Support From India | Android Online Job Support From India

Thank you for excellent article.

shiv said...

nice....................
vietnam web hosting
google cloud server hosting
canada telus cloud hosting
algeeria hosting
angola hostig
shared hosting
bangladesh hosting
botswana hosting
central african republi hosting
shared hosting

preethi minion said...

nice...
afghanistan hosting
angola hosting
afghanistan web hosting
bahrain web hosting
belize web hosting
india shared web hosting

preethi minion said...

italy web hosting
suden web hosting
tunisia hosting
uruguay web hosting
inplant training in chennai

Anonymous said...

I can's see the FTP server adress in your code. Where you tell the dotNet object the server or ip adress of the target?

chandhran said...

Good blog, its really very informative, do more blog under good concepts.
DOT NET Training in Bangalore
DOT NET Training in Chennai
DOT NET Training Institutes in Bangalore
DOT NET Course in Bangalore
Best DOT NET Training Institutes in Bangalore
DOT NET Institute in Bangalore
AWS Training in Bangalore
Data Science Courses in Bangalore
DevOps Training in Bangalore
PHP Training in Bangalore

shreekavi said...

Nice blog! Thanks for sharing this valuable information
Best IELTS Coaching in Bangalore
IELTS Training in Bangalore
IELTS Coaching centre in Chennai
IELTS Classes in Bangalore
IELTS Coaching in Bangalore
IELTS Coaching centre in coimbatore
IELTS Coaching in madurai
IELTS Coaching in Hyderabad
Selenium Training in Chennai
Ethical hacking course in bangalore

Microsoft azure training in Chennai said...

The database is an essential thing to do every MNC'S so Microsoft azure introduces cloud computing to store data in large amounts. Learn Microsoft Azure from leading institutes and secure your future well.

karthickannan said...

nice....
coronavirus update
inplant training in chennai
inplant training
inplant training in chennai for cse
inplant training in chennai for ece
inplant training in chennai for eee
inplant training in chennai for mechanical
internship in chennai
online internship

akshaya said...

This blog is instructive and this has aided the readers to know more about technology.Thanks for sharing this wonderful insight. Web Designing Course Training in Chennai | Web Designing Course Training in annanagar | Web Designing Course Training in omr | Web Designing Course Training in porur | Web Designing Course Training in tambaram | Web Designing Course Training in velachery

Janu said...

I would like to thank you for the efforts you had made for writing this awesome article. This article resolved my all queries. keep it up.


Dot Net Training in Chennai | Dot Net Training in anna nagar | Dot Net Training in omr | Dot Net Training in porur | Dot Net Training in tambaram | Dot Net Training in velachery


Jayalakshmi said...

Great efforts put it to find the list of articles which is very useful to know, Definitely will share the
same to other forums.
hadoop training in chennai

hadoop training in tambaram

salesforce training in chennai

salesforce training in tambaram

c and c plus plus course in chennai

c and c plus plus course in tambaram

machine learning training in chennai

machine learning training in tambaram

deiva said...

Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site...
web designing training in chennai

web designing training in omr

digital marketing training in chennai

digital marketing training in omr

rpa training in chennai

rpa training in omr

tally training in chennai

tally training in omr

praveen said...

Wow very informative blog,
Thanks and keep more updates,

oracle training in chennai

oracle training in porur

oracle dba training in chennai

oracle dba training in porur

ccna training in chennai

jeni said...

Thank you so much for sharing these amazing tips. I must say you are an incredible writer, I love the way that you describe the things. Please keep sharing.
hadoop training in chennai

hadoop training in velachery

salesforce training in chennai

salesforce training in velachery

c and c plus plus course in chennai

c and c plus plus course in velachery

machine learning training in chennai

machine learning training in velachery

vivekvedha said...

Nice blog! Thanks for sharing this valuable information.
acte chennai

acte complaints

acte reviews

acte trainer complaints

acte trainer reviews

acte velachery reviews complaints

acte tambaram reviews complaints

acte anna nagar reviews complaints

acte porur reviews complaints

acte omr reviews complaints

amc stars said...

دانلود کتاب صوتی مردی به نام اوه دانلود کتاب صوتی مردی به نام اوه
دانلود کتاب صوتی مردی به نام اوه دانلود کتاب صوتی مردی به نام اوه

Technogeekscs said...

This concept is a good way to enhance the knowledge.thanks for sharing..
Data Science
Selenium
ETL Testing

saketh321 said...

Your work is very good and I appreciate you and hopping for some more informative posts. ExcelR Data Science Course In Pune

jhansi said...


SAP stands for Systems Applications and Products in Data Processing. SAP, by definition, is also the name of the ERP (Enterprise Resource Planning) software as well as the name of the company.
tally training in chennai

hadoop training in chennai

sap training in chennai

oracle training in chennai

angular js training in chennai

Dr Galani said...

Dr. Vivek Galani is a leading expert in skin and hair. At hair transplant clinic in Surat Skin Care, Cosmetic Laser, Hair Transplant & Slimming Center, Dr. Galani offers the most advanced cosmetic and dermatologic care treatments. The clinic uses advanced FUE methods to produce high-quality hair transplants.

Oil Maker Machine said...

Shreeja Health Care is leading manufacturer of oil Maker Machine For Home. Shreeja Oil Extraction Machine is able to extract oil from various seeds like peanuts, Coconut, Sesame, Soybean, macadamia nuts, walnuts, sunflower seeds, vegetable seeds flaxseed et

sathishkumar said...

Thanks for sharing such nice info. I hope you will share more information like this. please keep on sharing!

Python Training In Bangalore | Python Online Training

Artificial Intelligence Training In Bangalore | Artificial Intelligence Online Training

Data Science Training In Bangalore | Data Science Online Training

Machine Learning Training In Bangalore | Machine Learning Online Training

AWS Training In Bangalore | AWS Online Training

IoT Training In Bangalore | IoT Online Training

Adobe Experience Manager (AEM) Training In Bangalore | Adobe Experience Manager (AEM) Online Training

Oracle Apex Training In Bangalore | Oracle Apex Online Training

Shreeja Health Care said...

Shreeja Health Care is leading manufacturer of Oil Maker Machine. Shreeja Oil Extraction Machine is able to extract oil from various seeds like peanuts, Coconut, Sesame, Soybean, macadamia nuts, walnuts, sunflower seeds, vegetable seeds flaxseed etc.

Radhapraveen said...


It’s a nice blog with very useful information!!!
Web Development courses in Chennai
PHP Training Institute in Chennai
Spoken English in Chennai
German Language Classes in Chennai
salesforce training institute in chennai
IELTS Training in Chennai

Sankarr76 said...


Good blog with necessary information only..!
Software Testing Training in Chennai
Software Testing Course in Bangalore
Software Testing Online Course

suryakumar said...

Thanks for the article. Its very useful. Keep sharing.
QTP Training
IOS Training
Informatica Training in Chennai
Web Designing course in Chennai

nayar said...

Amazing post.Thanks for sharing.........
IELTS Coaching in Hyderabad
IELTS Coaching in Bangalore
IELTS Coaching in Pune
IELTS Coaching in Gurgaon
IELTS Coaching in Delhi

chitra said...

Amazing post..Keep updating your blog
Digital Marketing Course in Chennai
Digital Marketing Training in Bangalore

Devi said...

Great post. keep sharing such a worthy information

cyber security course in bangalore
cyber security training in chennai

EZaccounting said...

I am very happy to see your article, awesome content, and keep updating.
E invoicing Singapore
Myob Singapore
Best Accounting software Singapore

Reshma said...


Wonderful post and more informative!keep sharing Like this!
salesforce institute in bangalore
Salesforce institute in bangalore

vivikhapnoi said...

This is one of the most incredible blogs Ive read in a very long time.
có vé máy bay từ mỹ về việt nam không

vé máy bay từ úc về việt nam giá rẻ

ve may bay tu han quoc ve viet nam

Gia ve may bay Vietnam Airline tu Nhat Ban ve Viet Nam

Gia ve may bay Vietnam Airline tu Dai Loan ve Viet Nam

cách đăng ký về việt nam từ canada

Hemapriya said...

Great Post!!! Thanks for the data update and waiting for your new updates.
what does .net framework do
do i need .net framework

Elango said...

Excellent Blog to read. You have shared a useful information. Thank you.
Ranorex Test Automation Online Certification
Ranorex Test Automation Online Training

bes unn said...



Hey friend, it is very well written article, thank you for the valuable and useful information you provide in this post. Keep up the good work! FYI, please check these depression, stress and anxiety related articles.
How to Build a Portfolio with ETFs, My vision for India in 2047 postcard, Essay on Unsung Heroes of Freedom Struggle

suganya said...

I really enjoyed with your post..thanks for sharing..Keep updating like this.no 1 embedded training institute in chennai
best embedded system software training institute in chennai

boopathy said...

Thanks for sharing.This is very useful blog.We want more updates from you.we also provide dot net training and projectsplease visitbest final year project center in chennai.

Morgan said...

If you need short-term access to a color laser printer, consulting with a printer rentals company is the best place to start. renting impresoras barcelona

Links For You said...

Rekordbox DJ Crack + Serial Key (2022) Full Version. Rekordbox DJ Crack is a powerful piece of software that can play and edit any type of. RekordBox Download Crackeado

Cyberz Pc said...

This production launched then nearly calls about researchers because of rapid research about 3D. Furthermore, this famous software is equipped because working of pragmatic tasks. Golden Software Surfer Crack

Silent Girl said...

At Independence day of Pakistan, Pakistani's shouldn't forget the sacrifices that people they have given at the time of partition. Web Site

BabuPrasad said...

Such an excellent and interesting blog, do post like this more with more information, this was very useful,erp development company in Germany. Thanks for sharing. Keep updating your blog.