我希望用户上传文件并将其保存到流中。c#上传文件到流?
这是迄今为止代码:
private void Submit_ServerClick(object sender, System.EventArgs e)
{
fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);
}
我希望用户上传文件并将其保存到流中。c#上传文件到流?
这是迄今为止代码:
private void Submit_ServerClick(object sender, System.EventArgs e)
{
fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);
}
你可以做这样的
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;
}
}
我希望它会帮助你...
的对象FileUpload.PostedFile
回报有InputStream
property可以读取上传的文件数据。
看起来这一个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.
}
你有什么问题呢?你卡在哪里?请阅读:http://msmvps.com/blogs/jon_skeet/archive/2010/08/29/writing-the-perfect-question.aspx – Oded
我想将它读入流中。我不知道在哪里可以从这里走。 – user999690