Sunday, November 29, 2009

Upload file from Document library to FTP Server

protected void PhotoUploadProcess()
{
try
{
// Get Current user and his last name
SPUser user = SPContext.Current.Web.CurrentUser;
username = user.Name;
smtpTo = user.Email;
strarray = username.Split(sepcomma);
userLastname = strarray[0].ToString();
//SPSecurity.RunWithElevatedPrivileges(delegate()
//{

//sharepoint site and web object
using (SPSite site = new SPSite(SPContext.Current.Site.Url.ToString() + "/Test"))
{
using (Microsoft.SharePoint.SPWeb web = site.OpenWeb())
{
SPList lstphoto = web.Lists["Test"];
web.AllowUnsafeUpdates = true;
string folderName;
int plenth = userLastname.Length - 1;
folderName = "Test" + "/" + userLastname.Substring(0, userLastname.Length - plenth);
SPFolder ImageFolder = web.GetFolder(folderName);


// Check whether sub folder is exist or not. If not, create folder in doc lib in user's last name first character format.
if (!ImageFolder.Exists)
{
SPFolderCollection myFolderCollection = web.GetFolder("PFPhyPic").SubFolders;
myFolderCollection.Add(userLastname.Substring(0, userLastname.Length - plenth));

}

// File input stream to read image and save it into document library
if (fileupload.PostedFile.ContentLength > 0)
{

Stream fStream;
SPFileCollection files = ImageFolder.Files;
fStream = fileupload.PostedFile.InputStream;
// Read file in a byte array
byte[] MyData = new byte[fStream.Length];
fStream.Read(MyData, 0, (int)fStream.Length);
strImagename = "123456.jpg";

SPFile filedata = files.Add(strImagename, MyData, true);
Int32 id = filedata.Item.ID;
strImagepath = SPContext.Current.Site.Url.ToString() + "/" + filedata.ServerRelativeUrl;

//update unique name
SPListItem lItemUser = null;
lItemUser = lstphoto.Items.GetItemById(id);
lItemUser["UniqueName"] = lblUID.Text;
lItemUser.Update();

lblMsg.Text = "Photo is uploaded successfully.";
btnCancel.Text = "Next =>";
// SPUtility.SendEmail(SPContext.Current.Web, false, false, useremailid, "Test Email", "Test");


//copy file to local server

try
{
System.IO.Directory.CreateDirectory("C:/FTPLOCALCOPY\\");
strlocalpath = @"C:/FTPLOCALCOPY/" + strImagename;

FileStream fs = new FileStream(strlocalpath, FileMode.Create);
BinaryWriter binaryWriter = new BinaryWriter(fs);

binaryWriter.Write(MyData);
binaryWriter.Close();

}
catch (Exception ex)
{

}

}

}
}
//});

}
catch (Exception ex)
{
lblMsg.Text = ex.Message.ToString();


}
}




public void uploadFileUsingFTP(string CompleteFTPPath, string CompleteLocalPath, string UName, string PWD, string domain)
{
try
{



//Create a FTP Request Object and Specfiy a Complete Path
FtpWebRequest reqObj = (FtpWebRequest)WebRequest.Create(CompleteFTPPath);

//Call A FileUpload Method of FTP Request Object
reqObj.Method = WebRequestMethods.Ftp.UploadFile;

//If you want to access Resourse Protected You need to give User Name and PWD
reqObj.Credentials = new NetworkCredential(UName, PWD, domain);

//FileStream object read file from Local Drive
FileStream streamObj = File.OpenRead(CompleteLocalPath);

//Store File in Buffer
byte[] buffer = new byte[streamObj.Length + 1];

//Read File from Buffer
streamObj.Read(buffer, 0, buffer.Length);




//Upload File to ftp://localHost/ set its object to nothing

reqObj.GetRequestStream().Write(buffer, 0, buffer.Length);

//Close FileStream Object Set its Value to nothing

streamObj.Close();

streamObj = null;



reqObj = null;
}
catch (Exception ex)
{
lblMsg.Text = ex.Message.ToString();


}

}