Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#1.0
Hi...already i got this link,plz help me to get code to upload a word document in c# version 1.1
Posted 13-Sep-11 3:18am
Edited 14-Sep-11 3:03am
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Look here:
http://msdn.microsoft.com/en-us/library/aa479405.aspx[^]
 
It works for uploading many other types of documents be it woird, excel pr whatever else.
 
Or:
 
<input id="myFile"  runat="server" type="file" />
 
//in your code behind
if ( myFile.Value != null && myFile.Value != ""  )
{
   HttpPostedFile file = myFile.PostedFile;
   String contentType = file.ContentType;
   Decimal fileSize = ( file.InputStream.Length / 1024 );
   String fileType = Path.GetExtension( file.FileName );
   if( fileType.Equals( "doc" ) || fileType.Equals( "docx" ) )
   {
       //Go ahead and proccess client request
       String filePath = Server.MapPath( "WordDocuments") + "/" + file.FileName;
       //save this to your database
      //WordDocuments in this case is the directory in your project root where you save your uploads
   }
   else
   {
       //alert user to select right file format
   }
}
else
{
    //alert user to select file first
}
 
Hope that helps Smile | :)
  Permalink  
v4
Comments
Mokshagna at 14-Sep-11 1:42am
   
Please,I want more clarified answer
Morgs Morgan at 14-Sep-11 13:24pm
   
See my example in my solution above and let me know if that's what you want?
Mokshagna at 15-Sep-11 3:56am
   
Thank u so...much Morgan!!!!....Its working fine :)
Morgs Morgan at 15-Sep-11 3:58am
   
Cool beans :)
Mokshagna at 15-Sep-11 6:12am
   
Hi...small doubt,here we need to save the path in database,how to save? its not saving
Morgs Morgan at 15-Sep-11 7:51am
   
Please see my solution above for changes.
Also please try to make an effort to google a little, this is a very simple task. Remember learning the "hard way" sticks in your head till you drop dead and getting "full code" like this will only help you once...
Mokshagna at 15-Sep-11 8:20am
   
ya...i understood,thanx for your sugggestion,now my application is running gud:)
Morgs Morgan at 15-Sep-11 8:47am
   
Great!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

using System.Io;
 

 private void saveFileToFolder()
        {
            try
            {
                string filepath = "";
                HttpPostedFile file = fileUploadTasks.PostedFile;
                FileEntity objFileEntity = new FileEntity();
 
                if (hiddenFolderPath.Value.ToString() != "")
                {
                    string fileExt = Path.GetExtension(file.FileName).ToLower();
                    int filelength = file.ContentLength;
                    string fileName = Path.GetFileName(file.FileName);
                    FileEntity objFile = objDocumentBusinessFacade.CheckExistingFileNameByFolderID(Convert.ToInt32(hdnFolderSelectionCount.Value), fileName);
                    if (objFile.FileID == 0)
                    {
                        if (fileName != string.Empty)
                        {
                            if (filelength < 1048576)//1MB
                            {
                                if (fileExt == ".txt" || fileExt == ".rtf" || fileExt == ".doc" || fileExt == ".docx" || fileExt == ".xls" || fileExt == ".xlsx" || fileExt == ".pdf" || fileExt == ".htm" || fileExt == ".html" || fileExt == ".ppt" || fileExt == ".pptx")
                                {
                                    filepath = hiddenFolderPath.Value.ToString() + "/" + fileName;
                                    file.SaveAs(Server.MapPath(hiddenFolderPath.Value.ToString() + "/") + fileName);
                                    objFileEntity.FilePath = filepath;
                                    objFileEntity.FileName = fileName;
                                    objFileEntity.FolderID = int.Parse(hiddenFolderID.Value);
                                    objDocumentBusinessFacade.InsertIntoFileandDocuments(objFileEntity);
                                }
                                else
                                {
                                    lblErrorMessage.Text = "Invalid file format!";
                                }
                            }
                            else
                            {
                                ScriptManager.RegisterStartupScript(this, this.GetType(), "AlertIndex", "alert('File size cannot exceed 1MB(1048576 bytes) ');", true);
                                return;
                            }
                        }
                    }
                    else
                    {
                        ScriptManager.RegisterStartupScript(this, this.GetType(), "AlertIndex", "alert('Given file name already exists in this folder');", true);
                        return;
                    }
                }
            }
            catch (Exception ex)
            {
                bool blnThrow = ExceptionPolicy.HandleException(ex, "WebUI");
                if (blnThrow)
                {
                    throw;
                }
            }
        }
  Permalink  
v2
Comments
Marcus Kramer at 14-Sep-11 9:36am
   
Please wrap code in "pre" blocks. I have added them to your post here to show you how it is done. Cheers.
Mokshagna at 15-Sep-11 8:30am
   
thanq:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Maciej Los 450
1 OriginalGriff 393
2 Sergey Alexandrovich Kryukov 229
3 CPallini 189
4 CHill60 180
0 OriginalGriff 6,092
1 Sergey Alexandrovich Kryukov 5,022
2 Maciej Los 3,269
3 Peter Leow 3,164
4 DamithSL 2,490


Advertise | Privacy | Mobile
Web03 | 2.8.140721.1 | Last Updated 15 Sep 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100