簡體   English   中英

如何上傳文件並將其保存在本地計算機的特定文件夾中?

[英]How to upload a file and save it in an specific folder in local computer?

我想將一個文件(上傳)到我在我的項目中創建的特定文件夾(在本地計算機上而不是服務器上!)。

這是我正在使用的代碼:

string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(Server.MapPath("images/" + filename));

我在一個按鈕中添加了文件上傳和上面的代碼。 但是代碼不起作用。 有什么問題?

我也使用了這種形式:

string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.SaveAs(Server.MapPath("~DesktopModules/UshtrimiDyte/images/" + filename));

我也將它與雙反斜杠一起使用,但這也不起作用。 我該如何解決我的問題?

試試上面的

 string path = Server.MapPath("~DesktopModules/UshtrimiDyte/images/" + filename);   
 System.IO.Stream myStream;
        Int32 fileLen;
        byte[] Input = null;
        fileLen = FileUpload1.PostedFile.ContentLength;
        if (fileLen > 0)
        {
            Input = new Byte[fileLen];
            myStream = FileUpload1.FileContent;
            myStream.Read(Input, 0, fileLen);
            string I_Docx_type = FileUploadFieldDoc.FileName.Substring(FileUpload1.FileName.LastIndexOf(".") + 1);
            WriteToFile(path +  "." +I_Docx_type, ref Input);
            path += "." + I_Docx_type;

        }

方法

 private void WriteToFile(string strPath, ref byte[] Buffer)
    {
        // Create a file
        System.IO.FileStream newFile = new System.IO.FileStream(strPath, System.IO.FileMode.Create);

        // Write data to the file
        newFile.Write(Buffer, 0, Buffer.Length);

        // Close file
        newFile.Close();

    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM