0

我正在尝试使用 HttpFileCollection 将视频文件上传到文件夹中,但它似乎没有保存在文件夹中。尽管路径正确,但它会给出一个错误,即找不到路径。这是我的代码

   <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#代码:

   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";

请帮我做这件事。

4

2 回答 2

3

试试这个代码。

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

            }
        }
于 2013-10-21T06:43:42.737 回答
0

参考以下代码:

您必须在代码中添加 for 循环,如下所示:

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

    {

    }
}
于 2013-10-21T06:21:38.443 回答