문제

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.

도움이 되었습니까?

해결책

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)
            {

            }
        }

다른 팁

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 )

    {

    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top