0

I have an export page that uses streamwriter :

            string _exportfilename = "RMAExportCustom.csv";
            string _exportpath = @"C:\" + _exportfilename;
            string line = "[REASON]^[OSD]^[ORDERID]^[ITEMS]^[NOTES]^[SHIPPER]^[DOW]";
            //string line = "IncidentID;OrderNum;Email;Name;OriginalShipDate;Notes;ReturnTagRequired;DateRetTagGenerated;RefundRequired;RefundAmount;DateRefundIssued;InventoryItem;InventoryQuantity;eBayCredit;eBayCreditFileDate;LostValue;IncidentReturnDate;Resolved;IncidentReceived;Shipping Cost;IncidentCreatedDate;RMA;RMARequested;RestockOption;Reason;ReceivedNotes;OurShippingCost;Resolution;Shipper;Creator" + Environment.NewLine;
            StreamWriter _writer = new StreamWriter(_exportpath);

            DateTime date = DateTime.Now;

            string cmdText = "SELECT ReasonDescription, OriginalShipDate, OrderID, STUFF((SELECT ', ' + IT.IncidentItem + ': ',IT.IncidentItemQTY FROM IncidentItems IT WHERE I.IncidentID = IT.IncidentID FOR XML PATH(''),TYPE).value('.','VARCHAR(MAX)')  ,1,1,'') AS Items, Notes, ShipperName, (SELECT DATENAME(WEEKDAY, OriginalShipDate)) AS [DOW] FROM [Incidents] AS I INNER JOIN IncidentReasons Reason on I.IncidentReasonID = Reason.IncidentReasonID INNER Join ShipperID S on I.ShipperID = S.ShipperID Order By IncidentID Desc";
            SqlConnection connection = new SqlConnection(CONNECTIONSTRING);
            SqlCommand cmd = new SqlCommand(cmdText, connection);
            SqlDataReader rdr = null;
            connection.Open();
            rdr = cmd.ExecuteReader();

            _writer.WriteLine(line);
            line = "";

            while (rdr.Read())
            {
                line = rdr["ReasonDescription"] + "^" + rdr["OriginalShipDate"] + "^" + rdr["OrderID"] + "^" + rdr["Items"] + "^" + rdr["Notes"] + "^" + rdr["ShipperName"] + "^" + rdr["DOW"];
                _writer.WriteLine(line);
                line = "";
            }
            connection.Close();
            _writer.Close();

That file is saved to the c: drive which is probably bad and I have the output stream, so the user can save it somewhere else.

            this.Response.Clear();
            this.Response.ContentType = "application/octet-stream";
            this.Response.AddHeader("Content-Disposition", "attachment; filename=\"" + _exportfilename + "\"");

            this.Response.WriteFile(_exportpath);
            this.Response.Flush();
            this.Response.End();

This worked 2 years ago on windows 7 machines and I allowed IIS permissions on the users entire c: drive but we've moved on. I don't think I want to go to every computer and open up permissions to their entire drive. Also this whole part of saving to the drive first then allowing output stream to save it somewhere different is not what I should be doing.

Hopping for a rubber ducking moment but got to the end to no avail.

zerodoc
  • 393
  • 1
  • 4
  • 16
  • `I don't think I want to go to every computer and open up permissions`. I gotta say I am totally stumped what you are asking. Could it be that you are running a web application locally on every user's machine? – NightOwl888 Feb 01 '18 at 15:39
  • A asp.net internal web application. When I say open up permissions I mean go to our client computers internally on a local network and grant permission to the entire c: drive so asp.net users can save a file. I'm only trying to generate a file to save to the users computer. – zerodoc Feb 01 '18 at 16:06
  • Why not simply use a `StreamWriter` to write the file content to the Response stream instead of writing it to a file? – NightOwl888 Feb 01 '18 at 16:35
  • Is there an example of this? I can't seem to find a decent one. – zerodoc Feb 01 '18 at 16:48
  • 1
    There is a pretty good [example of writing to OutputStream here](https://stackoverflow.com/a/13456219/). Just use the `StreamWriter` inside of the `GenerateExportFile` method to output your file directly to the browser. – NightOwl888 Feb 01 '18 at 16:55
  • I went with your suggestion I just need to get a file export one last time. This project is getting overhauled. – zerodoc Feb 01 '18 at 18:56

0 Answers0