none
File Upload web part

    Question

  • Hi,

    Can anyone guide me as in how to develop a FileUpload web part with a Browse Button.

    Or is this readily available in Sharepoint ?
    Monday, April 09, 2007 9:24 PM

Answers

  • Guys,

     

    Following is the way i achieved this feature

    1. Created an aspx page with html control (Input:file) and a button to upload the document

    2.The html control (Input:file) is used to achieve the file-dialog window in the form

    3. On the click event of the upload button i added code (that leveraged from Sharepoint API) to upload the docuemnt

    4. Finally i wrapped this page inside a page viewer webpart and made available  to the sharepoint world

     

    The click-event of the button looks like the following

    protected void btnUpload_Click(object sender, EventArgs e)

    {

    string error;

    if (File1.PostedFile.FileName =="" || File1.PostedFile.ContentLength < 1)

    {

    lblStatus.Text="Enter a valid file";

    return;

    }

    if (!File1.PostedFile.FileName.ToLower().EndsWith(".doc"))

    {

    lblStatus.Text = "Only Microsoft Word documents are allowed";

    return;

    }

    Stream iStream = File1.PostedFile.InputStream;

    int basenamestart = File1.PostedFile.FileName.LastIndexOf(@"\");

    string fname = File1.PostedFile.FileName.Substring(basenamestart +1);

    if (iStream!=null)

    {

    try

    {

    string status = UploadToDoclibrary(iStream,fname,Request.LogonUserIdentity.Name);

    lblStatus.Text = status;

    }

    catch (Exception ex)

    {

    EventLog.WriteEntry("ManuscriptUpload","FileName : " + fname + "\n" + ex.Message + "\n" + ex.StackTrace);

    lblStatus.Text="An Error occurred during upload. Please try later.";

    }

    }

    }

     

    private static string UploadToDoclibrary(Stream inputStream,string filename,string username)

    {

    SPSite mySiteCollection = new SPSite(ConfigurationManager.AppSettings["DocLibrarySite"]);

    SPWeb mySite = mySiteCollection.AllWebs[ConfigurationManager.AppSettings["DocLibraryWeb"]];

    mySite.AllowUnsafeUpdates = true;

    SPFolder ctcFolder = mySite.GetFolder(ConfigurationManager.AppSettings["DocLibrary"]);

    if (!exists(filename,ctcFolder,username))

    {

    ctcFolder.Files.Add(filename, inputStream);

    ctcFolder.Update();

    }

    else

    return "You have already uploaded your manuscript!";

    mySite.Close();

    return "File Uploaded Successfully.";

    }

     

    I was about to produce a quick solution. That's why i've chosen .aspx page and wrapped inside a page viewer webpart of sharepoint. The best way is to wrap the entire code in a webpart and deploy to sharepoint

     

     

    Friday, April 13, 2007 2:57 AM

All replies

  • Hi,

    I'm searching for this answer as well! I'm still very very new to sharepoint and hope someone can help! Sad

    Any takers~?   
    Thursday, April 12, 2007 3:58 AM
  • Hello Guys,

     

    I've the answer with sample code. I'll post that after an hour. I'm heading for an important meeting now

    Thursday, April 12, 2007 6:58 AM
  • Guys,

     

    Following is the way i achieved this feature

    1. Created an aspx page with html control (Input:file) and a button to upload the document

    2.The html control (Input:file) is used to achieve the file-dialog window in the form

    3. On the click event of the upload button i added code (that leveraged from Sharepoint API) to upload the docuemnt

    4. Finally i wrapped this page inside a page viewer webpart and made available  to the sharepoint world

     

    The click-event of the button looks like the following

    protected void btnUpload_Click(object sender, EventArgs e)

    {

    string error;

    if (File1.PostedFile.FileName =="" || File1.PostedFile.ContentLength < 1)

    {

    lblStatus.Text="Enter a valid file";

    return;

    }

    if (!File1.PostedFile.FileName.ToLower().EndsWith(".doc"))

    {

    lblStatus.Text = "Only Microsoft Word documents are allowed";

    return;

    }

    Stream iStream = File1.PostedFile.InputStream;

    int basenamestart = File1.PostedFile.FileName.LastIndexOf(@"\");

    string fname = File1.PostedFile.FileName.Substring(basenamestart +1);

    if (iStream!=null)

    {

    try

    {

    string status = UploadToDoclibrary(iStream,fname,Request.LogonUserIdentity.Name);

    lblStatus.Text = status;

    }

    catch (Exception ex)

    {

    EventLog.WriteEntry("ManuscriptUpload","FileName : " + fname + "\n" + ex.Message + "\n" + ex.StackTrace);

    lblStatus.Text="An Error occurred during upload. Please try later.";

    }

    }

    }

     

    private static string UploadToDoclibrary(Stream inputStream,string filename,string username)

    {

    SPSite mySiteCollection = new SPSite(ConfigurationManager.AppSettings["DocLibrarySite"]);

    SPWeb mySite = mySiteCollection.AllWebs[ConfigurationManager.AppSettings["DocLibraryWeb"]];

    mySite.AllowUnsafeUpdates = true;

    SPFolder ctcFolder = mySite.GetFolder(ConfigurationManager.AppSettings["DocLibrary"]);

    if (!exists(filename,ctcFolder,username))

    {

    ctcFolder.Files.Add(filename, inputStream);

    ctcFolder.Update();

    }

    else

    return "You have already uploaded your manuscript!";

    mySite.Close();

    return "File Uploaded Successfully.";

    }

     

    I was about to produce a quick solution. That's why i've chosen .aspx page and wrapped inside a page viewer webpart of sharepoint. The best way is to wrap the entire code in a webpart and deploy to sharepoint

     

     

    Friday, April 13, 2007 2:57 AM
  • Nice thank you very much, this post is going in the bookmark :-D
    Friday, April 13, 2007 7:05 AM
  • Thanks a lot...let me try this out...
    Friday, April 13, 2007 12:52 PM
  • Hi Sundar,

    Thanks for the code. I am getting error at the following statement..i.e. " The name exists does not exist in the current context".

    Could you please throw some light on "!exists" ?? Is it a function which you are calling?


    if (!exists(filename,ctcFolder,username))

    {

    ctcFolder.Files.Add(filename, inputStream);

    ctcFolder.Update();

    }


    Thanks,

    Amit

    Wednesday, September 12, 2007 11:35 PM
  •  

    How do you go about wraping asp code into a sharepoint web part
    Thursday, November 01, 2007 6:48 PM
  • Today classic asp has become legacy code. It's not advisable to go for classic ASP. However ASP.NET provides a control called 'asp:fileupload'. You can build a webpart using this ASP.NET control also.

     

    Please refer the following urls to know more about asp:fileupload control :

     

    http://msdn2.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.aspx

    http://www.c-sharpcorner.com/UploadFile/mahesh/FileUpload10092005172118PM/FileUpload.aspx?ArticleID=79850d6d-0e91-4d7b-9e27-a64a09b0ee6b

    http://www.wrox.com/WileyCDA/Section/id-292158.html

     

    Sunday, November 04, 2007 5:33 AM
  • Hi ,

    I have the same requirement , i created application page in sharepoint designer with fileupload control ( inside a form view control)  to save the file in to my Custom databse.

    the same functionality is working fine in .net ,but when it comes to sharepoint fileupload is not happing ,its taking as null values .

     

    Thanks in adavnce 

    Giriraj

    Wednesday, November 10, 2010 2:20 PM
  • Hi ,

    I have the same requirement , i created application page in sharepoint designer with fileupload control ( inside a form view control)  to save the file in to my Custom databse.

    the same functionality is working fine in .net ,but when it comes to sharepoint fileupload is not happing ,its taking as null values .

     

    Thanks in adavnce 

    Giriraj

    Monday, November 15, 2010 6:39 AM
  • the same functionality is working fine in .net ,but when it comes to sharepoint fileupload is not happing ,its taking as null values .

    Check enctype attribute value in form node. Should be multipart/form-data

    <FORM action="http://server.com/cgi/handle" enctype="multipart/form-data" method="post"> <P> What is your name? <INPUT type="text" name="submit-name"><BR> What files are you sending? <INPUT type="file" name="files"><BR> <INPUT type="submit" value="Send"> <INPUT type="reset"> </FORM>

    http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2

     

     

     

    Monday, November 15, 2010 7:15 AM
  • Hi  I tried with html inputfile

    <INPUT type="file" id="samplefile" runat="server" /> ,inside a form view control ,i have written code like this

    informview insermode:

    <input  id="filMyFile"  type="file" runat="server" />  


    inline code:

    protected void FormView2_ItemInserting1(object sender, FormViewInsertEventArgs e)

        {

                string filename = "";

     HtmlInputFile fileMyFile = (HtmlInputFile)FormView2.FindControl("filMyFile");          

                 Byte[] imgByte = null;

                 if (fileMyFile.PostedFile != null)

                {

                    HttpPostedFile myFile = fileMyFile.PostedFile;

                    string filepath = fileMyFile.PostedFile.FileName;

                    filename=Path.GetFileName(filepath);

                    string ext=Path.GetExtension(filename);

                    imgByte =new Byte[myFile.ContentLength];

                    myFile.InputStream.Read(imgByte,0,myFile.ContentLength);

                    FileStream fs = new FileStream(filepath, FileMode.Open, FileAccess.Read);

                    BinaryReader br = new BinaryReader(fs);

                    Byte[] bytes = br.ReadBytes((Int32)fs.Length);              

                    }

    }

     

     

    but still its not wroking...

    Tuesday, November 16, 2010 7:08 AM