none
自動生成folder和save檔案 RRS feed

  • 問題

  • 我想透過dropdownlist既資料生出一層一層的folder路徑, 從而上傳檔案到該路徑, 以下是我的code,
    問題出在哪裡, 它不懂自動生folder...以及上傳檔案該怎樣寫!?

    謝謝!

    private void button1_Click(object sender, EventArgs e)
            {
                ofd.InitialDirectory = "C:\\";
                ofd.Filter = "All files (*.*)|*.*|Pdf file(*.pdf)|*.pdf";
                ofd.FilterIndex = 1;
                ofd.RestoreDirectory = true;

                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    txtpath.Text = ofd.FileName;
                    //之後想幹嗎就干嗎
                    if (txtpath.Text == null)
                    {
                        // No file uploaded!
                        lbluploadde.Text = "Please first select a file to upload...";

                        if (string.Compare(System.IO.Path.GetExtension(ofd.FileName), ".pdf", true) != 0)
                        {
                            lbluploadde.Text = "Only PDF documents may be used for a category's brochure.";
                            lbluploadde.Visible = true;
                            //e.Cancel;
                            return;
                        }
                    }

                    else
                    {
                        // Display the uploaded file's details
                        lbluploadde.Text = string.Format(
                        @"Uploaded file: {0}",
                            //File size (in bytes): {1:N0}<br />
                            // Content-type: {2}",

                            ofd.FileName);
                        //ofd.FileBytes.Length,
                        //ofd.PostedFile.ContentType);
                    }

                }
            }

            private void btnUpload_Click(object sender, EventArgs e)
            {


                if (txtpath.Text == "")
                {
                    // No file uploaded!
                    lbluploadde.Text = "Please first select a file to upload...";
                }

                string dir1 = "~/UpLoadFile/" +
                ddlYear.SelectedItem + "/" +
                ddlCourse.SelectedItem + "/" +
                ddlLvSemModule.SelectedItem + "/" +
                ddlCat.SelectedItem;//定義相對路徑

                if (System.IO.Directory.Exists(dir1) == false)
                {
                    System.IO.Directory.CreateDirectory("~/UpLoadFile/" +
                ddlYear.SelectedItem + "/" +
                ddlCourse.SelectedItem + "/" +
                ddlLvSemModule.SelectedItem + "/" +
                ddlCat.SelectedItem+ "/" +
                ddlValue.SelectedItem);//(沒有就先建父的,再建子文件夾)
                }
                else
                {
                    // Save the file
                    string filePath =
                        ("~/UpLoadFile/" +
                        ddlYear.SelectedItem + "/" +
                    ddlCourse.SelectedText + "/" +
                     ddlLvSemModule.SelectedText + "/" +
                     ddlCat.SelectedText + "/" +
                     ddlValue.SelectedText +
                     ofd.FileName);


                    sfd..Equals(filePath);
                }

            }

    2010年5月5日 下午 05:13

所有回覆

  • 目錄要一層層建,你想偷懶就寫遞回函數來建。
    論壇是網友平等互助 保證解答請至 微軟技術支援服務
    提問時,錯誤情境描述與錯誤訊息很重要,情境描述包含你做了什麼,預期的結果與實際發生的結果。一個最爛的問法範例:「我的電腦電腦怎麼不能開機?」誰知道你家是不是沒電還是你根本找不到電源鈕。
    2010年5月6日 下午 02:03