0

I am trying to upload video file in a folder using HttpFileCollection but it appears that it is not getting saved in the folder. It is giving an error that it cannot find the path although the path is correct. This is my code

   <form id="form1" runat="server">
<div>

    <asp:Image ID="Image1" ImageUrl="~/ProductVideos/" runat="server" Width="118px" />

</div>
    <asp:FileUpload ID="FileUpload1" runat="server" />
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
</form>

C# Code:

   protected void Button1_Click(object sender, EventArgs e)
    {
        UploadVideoToFolder();
    }

     public string UploadVideoToFolder()
    {
        string vTitle = "";
        string vDesc = "";
        string FilePath = HttpContext.Current.Server.MapPath("~/ProductVideos/" + HttpContext.Current.Request.Form["title"]);



        HttpFileCollection MyFileCollection = HttpContext.Current.Request.Files;
        if (MyFileCollection.Count > 0)
        {
            // Save the File
            try
            {
                MyFileCollection[0].SaveAs(FilePath);
                return "1";
            }
            catch (Exception ex)
            {
                return "-1";
            }
        }
        else
            return "-1";

Help me do this please.

Manu
  • 418
  • 3
  • 8
  • 20
  • Do you have debug post the exception message attach with this question? It would be help to identify the problem. – Toan Vo Oct 21 '13 at 06:20
  • Yeah. It says it cannot find the path although the path is correct. It is giving this error when i try to save the file – Manu Oct 21 '13 at 06:23

2 Answers2

3

Try this code.

        string filename = Path.GetFileName(FileUpload1.FileName);
        HttpFileCollection MyFileCollection = HttpContext.Current.Request.Files;
        if (MyFileCollection.Count > 0)
        {
            try
            {
                MyFileCollection[0].SaveAs(Server.MapPath("~/ProductVideos/") + filename);

             }
            catch (Exception ex)
            {

            }
        }
threeleggedrabbit
  • 1,722
  • 2
  • 28
  • 60
  • Ok.. The problem is i am getting video using wcf. So i want this string FilePath = HttpContext.Current.Server.MapPath("~/ProductVideos/" + HttpContext.Current.Request.Form["title"]); and having problem in this only. Help me solve this. – Manu Oct 21 '13 at 06:56
  • Is that HttpContext.Current.Request.Form["title"] returning value. Please see this http://stackoverflow.com/questions/18191908/httpcontext-current-request-form-checkbox – threeleggedrabbit Oct 21 '13 at 07:06
0

Refer Following Code:

You will have to add for loop in your code as follows:

HttpFileCollection MyFileCollection  = Request.Files;
for (int i = 0; i < MyFileCollection .Count; i++)
{
    HttpPostedFile httpPostedFile = MyFileCollection [i];
    if (httpPostedFile.ContentLength > 0 )

    {

    }
}
C Sharper
  • 8,284
  • 26
  • 88
  • 151
  • No, it's giving the same error. The error is "could not find the path" although the path is correct – Manu Oct 21 '13 at 06:38