hi..
I want to add my PDF file in my website folder PDF File. Which code i should use?
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Om prakash Singh126 reputationPosted Jul 25, 2011, 9:06 PM EDT
On code behind, at the button click event write this code
private void cmdUpload_Click(object sender, System.EventArgs e)
{
string myFolder = "PDF"
if(base.Settings.fldAdsFolder.Length > 0)
{
string uploadPath = base.MappedApplicationPath + myFolder + @"\";
this.litUploadError.Text = "Your file has been posted to the server.";
//before actual upload so upload func can overwrite if error.
UploadPDF(uploadPath);
}
else
{
this.litUploadError.Text = "You must set the Ads Folder in Settings first.";
}
}
private bool UploadPDF(string UploadPath)
{
bool retVal = true;
string strFileName;
string strFilePath;
string strType;
string strFolder = UploadPath; //save it here
string err = "";
string ext = "";
if(filUpload.Value != "")
{
strFileName = base.GetFileName(filUpload.PostedFile.FileName);
strType = filUpload.PostedFile.ContentType;
// Create the directory if it does not exist.
if(!Directory.Exists(strFolder))
{
string strFolderToCreate = strFolder;
if(strFolder.EndsWith(@"\"))
strFolderToCreate = strFolder.Substring(0,(strFolderToCreate.Length-1));
Directory.CreateDirectory(strFolderToCreate);
}
// Save the uploaded file to the server.
strFilePath = strFolder + strFileName;
if(File.Exists(strFilePath))
{
//only shows file name to user
err = strFileName + " already exists on the server!";
}
else
{
filUpload.PostedFile.SaveAs(strFilePath);
}
}
}
if(err.Trim().Length > 0)
{
this.litUploadError.Text = err.Trim();
retVal = false;
}
return retVal; //if it returns as "false" don't continue to save.
}
//You should really stick this function in a different class where you can reuse it better.
public static string MappedApplicationPath
{
get
{
string APP_PATH = System.Web.HttpContext.Current.Request.ApplicationPath.ToLower();
if(APP_PATH == "/") //a site
APP_PATH = "/";
else if(!APP_PATH.EndsWith(@"/")) //a virtual
APP_PATH += @"/";
string it = System.Web.HttpContext.Current.Server.MapPath(APP_PATH);
if(!it.EndsWith(@"\"))
it += @"\";
return it;
}
}
Jiteendra SampathiraoRank #585 · 4.1K reputationPosted Jul 25, 2011, 9:08 PM EDT
HttpPostedFile inputFile = myFileControl.PostedFile;byte[] byteArray =newbyte[inputFile.ContentLength];inputFile.InputStream.Read(byteArray,0.inputFile.ContentLength);WriteFileToDir(byteArray,"\\YourDirectoryPathHere");privatevoidWriteFileToDir(refbyte[] Buffer,stringstrFolderPath){FileStream fileStream =newFileStream(strFolderPath, FileMode.Create);fileStream.Write(Buffer, 0, Buffer.Length);fileStream.Close();}
Source: Eggheadcae
hope this help you....