c# - 如何上传文件并将其保存在本地计算机的特定文件夹中?

我想将文件上传(上传)到我在项目中创建的特定文件夹(在本地计算机上而不是服务器上!)。

这是我使用的代码:

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();

    }

https://stackoverflow.com/questions/17142100/

相关文章:

javascript - Shopify 'View all' 按钮实现

node.js - 为什么process.memoryUsage()不输出 Node 进程消耗的内存

java - 创建 keystore 时出错 => 线程 "main"java.io.DataInp

qt - qml listview提高改变项目的速度

machine-learning - 最先进的增量决策树学习者

python - 在 python 解释器和脚本文件中运行时的不同结果

maven - 如何在 ftp 中部署 Maven Web 应用程序?

google-chrome - CORS 错误仅发生在 Chrome 中的某些 XHR 上

css - 使用渐变生成饱和度/亮度蒙版

asp.net - URL 重写出站规则和自定义错误页面