繁体   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