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.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Jiteendra SampathiraoPosted Jul 26, 2011, 1:08 AM
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....
Om prakash SinghPosted Jul 26, 2011, 1:06 AM
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;
}
}