简体   繁体   中英

write and read from byte stream

I have a page where the User can either upload their own csv or enter values into a listbox which then creates a csv (in the background). Regardless of which way the csv gets created I need to upload that csv to our server via a byte stream.

My problem is that when Im creating the csv I shouldn't have to create a temporary file, I should be able to write to the stream then read it back for uploading. How can I remove the need for the temporary file?

current code which works (but uses temp file):

try {
                string filename = DateTime.Now.ToString("MMddyyHmssf");
                filename = filename + ".csv";
                string directory = ConfigurationManager.AppSettings["TempDirectory"].ToString();
                path = Path.Combine(directory, filename);
                using (StreamWriter sw = File.CreateText(path)) {

                    foreach (ListItem item in this.lstAddEmailAddress.Items) {
                        sw.WriteLine(" , ," + item.ToString());
                    }
                }
            } catch (Exception ex) {
                string error = "Cannot create temp csv file used for importing users by email address.  Filepath: " + path + ".  FileException: " + ex.ToString();
                this.writeToLogs(error, 1338);
            }
        }
        // put here for testing the byte array being sent vs ready byte[] byteArray = System.IO.File.ReadAllBytes(path);
        myCsvFileStream = File.OpenRead(path);
        nFileLen = (int)myCsvFileStream.Length;

I have tried

Stream myCsvFileStream;
using (StreamWriter sw = new StreamWriter(myCsvFileStream)) {

                    foreach (ListItem item in this.lstAddEmailAddress.Items) {
                        sw.WriteLine(" , ," + item.ToString());

                    }
                }

However since myCsvFileStream is not initialized (because stream is a static class) it is always null.

Here is what I do with the data (byte stream) after creating the csv.

byte[] file = new byte[nFileLen];
            myCsvFileStream.Read(file, 0, nFileLen);
            bool response = this.repositoryService.SaveUsers(this.SelectedAccount.Id, file, this.authenticatedUser.SessionToken.SessionId);
            myCsvFileStream.Close();

In the end I used StringBuilder to create my csv file contents. Then got a byte array of its contents and used that to populate my shared stream (I say shared because when the user enters their own CSV file it is a HttpPostedFile but when sending it to our server via the rest call ( respositoryservices.saveusers ) it uses the same byte stream that it would via this method)

StringBuilder csvFileString = new StringBuilder();

            sharedStreamForBatchImport = new MemoryStream();
            foreach (ListItem item in this.lstAddEmailAddress.Items) {
                csvFileString.Append(",," + item.ToString() + "\\r\\n");
            }

            //get byte array of the string
            byteArrayToBeSent = Encoding.ASCII.GetBytes(csvFileString.ToString());
            //set length for read
            byteArraySize = (int)csvFileString.Length;
            //read bytes into the sharedStreamForBatchImport (byte array)
            sharedStreamForBatchImport.Read(byteArrayToBeSent, 0, byteArraySize);

你想创建一个new MemoryStream()

Here is a function I use to write CSV files

    public static bool WriteCsvFile(string path, StringBuilder stringToWrite)
    {
        try
        {
            using (StreamWriter sw = new StreamWriter(path, false))         //false in ordre to overwrite the file if it already exists
            {
                sw.Write(stringToWrite);
                return true;
            }
        }
        catch (Exception)
        {
            return false;
        }
    }

stringToWrite is just a string that has been created that way :

    public static bool WriteCsvFile(string path, DataTable myData)
    {
        if (myData == null)
            return false;
        //Information about the table we read
        int nbRows = myData.Rows.Count;
        int nbCol = myData.Columns.Count;
        StringBuilder stringToWrite = new StringBuilder();

        //We get the headers of the table
        stringToWrite.Append(myData.Columns[0].ToString());
        for (int i = 1; i < nbCol; ++i)
        {
            stringToWrite.Append(",");
            stringToWrite.Append(myData.Columns[i].ToString());
        }
        stringToWrite.AppendLine();

        //We read the rest of the table
        for (int i = 0; i < nbRows; ++i)
        {
            stringToWrite.Append(myData.Rows[i][0].ToString());
            for (int j = 1; j < nbCol; ++j)
            {
                stringToWrite.Append(",");
                stringToWrite.Append(myData.Rows[i][j].ToString());
            }
            stringToWrite.AppendLine();
        }

        return WriteCsvFile(path, stringToWrite);
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM