c# 通过文件流方式上传文件到服务器

public string UploadFile(string userID)
    {
        Byte[] MeaningFile;
        FileStream stream = new FileStream(tbFilePath.Value, FileMode.Open, FileAccess.Read);
        int size = Convert.ToInt32(stream.Length);
        MeaningFile = new Byte[size];
        stream.Read(MeaningFile, 0, size);
        stream.Close();
        FileStream fos = null;

        string fileName = "";
        string fileExt = tbFilePath.Value.Substring(tbFilePath.Value.LastIndexOf("."));
        if (fileExt == ".xls")//必须是EXCEL文件
        {
            try
            {
                string filePath = Server.MapPath("~/DataFile/");
                if (!Directory.Exists(filePath))
                {
                    Directory.CreateDirectory(filePath);
                }
                fileName = userID + fileExt;
                fos = new FileStream(filePath + fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                fos.Write(MeaningFile, 0, MeaningFile.Length);
                fos.Close();
            }
            catch
            {
            }
            finally
            {
                if (fos != null)
                {
                    try
                    {
                        fos.Close();
                    }
                    catch
                    {
                    }
                }
            }
        }
        else
        {

        }
        return fileName;
    }

你可能感兴趣的:(.net)