C# 上传文件到流?

发布于 2024-12-10 14:15:35 字数 237 浏览 0 评论 0原文

我希望用户上传文件并将其保存到流中。

这是到目前为止的代码:

 private void Submit_ServerClick(object sender, System.EventArgs e)
        {


               fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);

        }

I want the user to upload a file and save it to a stream.

Here is the code so far:

 private void Submit_ServerClick(object sender, System.EventArgs e)
        {


               fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);

        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

_畞蕅 2024-12-17 14:15:35

你可以这样做

  string filePath = uploadFile(fileUploadControl.FileContent);

private string uploadFile(Stream serverFileStream)
{
    string filename = ConfigurationManager.AppSettings["FileUploadTempDir"] + 
    DateTime.Now.ToString("yyyyMMddhhmm") + "_" + 
    Customer.GetCustomerName(CustomerId).Replace(" ", "_") + ".txt";

   try
   {
    int length = 256;
    int bytesRead = 0;
    Byte[] buffer = new Byte[length];

    // write the required bytes
    using (FileStream fs = new FileStream(filename, FileMode.Create))
    {
        do
        {
            bytesRead = serverFileStream.Read(buffer, 0, length);
            fs.Write(buffer, 0, bytesRead);
        }
        while (bytesRead == length);
    }

    serverFileStream.Dispose();
    return filename;
  }
  catch (Exception ex)
  {
    lblErrorMessage.Text += "An unexpeded error occured uploading the file. " + ex.Message;
    return string.Empty;
  }
}

我希望它能帮助你......

you could do like this

  string filePath = uploadFile(fileUploadControl.FileContent);

private string uploadFile(Stream serverFileStream)
{
    string filename = ConfigurationManager.AppSettings["FileUploadTempDir"] + 
    DateTime.Now.ToString("yyyyMMddhhmm") + "_" + 
    Customer.GetCustomerName(CustomerId).Replace(" ", "_") + ".txt";

   try
   {
    int length = 256;
    int bytesRead = 0;
    Byte[] buffer = new Byte[length];

    // write the required bytes
    using (FileStream fs = new FileStream(filename, FileMode.Create))
    {
        do
        {
            bytesRead = serverFileStream.Read(buffer, 0, length);
            fs.Write(buffer, 0, bytesRead);
        }
        while (bytesRead == length);
    }

    serverFileStream.Dispose();
    return filename;
  }
  catch (Exception ex)
  {
    lblErrorMessage.Text += "An unexpeded error occured uploading the file. " + ex.Message;
    return string.Empty;
  }
}

i hope it will helps you...

又怨 2024-12-17 14:15:35

FileUpload.PostedFile 返回的对象具有 InputStream 属性 可以从中读取上传的文件数据。

The object that FileUpload.PostedFile returns has an InputStream property you can read the uploaded file data from.

本宫微胖 2024-12-17 14:15:35

看起来像这个 http://support.microsoft.com/kb/323246

    string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);
string SaveLocation = Server.MapPath("Data") + "\\" +  fn;
try
{
    File1.PostedFile.SaveAs(SaveLocation);
    Response.Write("The file has been uploaded.");
}
catch ( Exception ex )
{
    Response.Write("Error: " + ex.Message);
    //Note: Exception.Message returns a detailed message that describes the current exception. 
    //For security reasons, we do not recommend that you return Exception.Message to end users in 
    //production environments. It would be better to put a generic error message. 
}

Looks like this one http://support.microsoft.com/kb/323246

    string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);
string SaveLocation = Server.MapPath("Data") + "\\" +  fn;
try
{
    File1.PostedFile.SaveAs(SaveLocation);
    Response.Write("The file has been uploaded.");
}
catch ( Exception ex )
{
    Response.Write("Error: " + ex.Message);
    //Note: Exception.Message returns a detailed message that describes the current exception. 
    //For security reasons, we do not recommend that you return Exception.Message to end users in 
    //production environments. It would be better to put a generic error message. 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文