I have a WCF web service that returns a stream. At the client side when i try to read it using below code then i get an exception at line " Byte[] buffer = new Byte[outputMessage.FileByteStream.Length];" saying System.Notsupported. Please advise me on what am i doing wrong here.
FileMetaData metaData = new FileMetaData();
metaData.ProductIDsArray = new string[] { "1", "2" };
metaData.AuthenticationKey = "test";
FileDownloadMessage inputParam = new FileDownloadMessage(metaData);
FileTransferServiceClient obj = new FileTransferServiceClient();
FileDownloadReturnMessage outputMessage = obj.DownloadFile(inputParam);
Byte[] buffer = new Byte[outputMessage.FileByteStream.Length];
int byteRead = outputMessage.FileByteStream.Read(buffer, 0, buffer.Length);
Response.Buffer = false;
Response.Buffer = false;
Response.ContentType = "application/x-zip";
Response.AppendHeader("content-length", buffer.Length.ToString());
Stream outStream = Response.OutputStream;
while (byteRead > 0)
{
outStream.Write(buffer, 0, buffer.Length);
byteRead = outputMessage.FileByteStream.Read(buffer, 0, buffer.Length);
}
outputMessage.FileByteStream.Close();
outStream.Close();
The stream that you're reading from does not support getting the length of the stream (most likely 'cause the length will not be known until the entire file has been downloaded). Read the stream in chunks - similar to how the while loop is doing, but have a fixed size buffer - once you get 0 returned for byteRead you'll know you've hit the end-of-stream.
Related
while generating pdf on localhost using below code is fine,however it generates a pdf of 0KB size while on server . What I am doing wrong here ?
I am using iTextsharp nuget and the code runs fine on local server.
public byte[] GetPDF(string pHTML, List<int> ideaidlist)
{
byte[] bPDF = null;
MemoryStream ms = new MemoryStream();
TextReader txtReader = new StringReader(pHTML);
// Document doc = new Document(PageSize.LETTER, 0, 0, 0, 0);
Document doc = new Document(new Rectangle(864f, 870f), 0, 0, 0, 0);
string Certpath = ConfigurationManager.AppSettings["MailImagePath"]+"Certificates.pdf";//System.Configuration.
string ImgTopPath =ConfigurationManager.AppSettings["CertificateImagePath"];
string ImgMidPath =ConfigurationManager.AppSettings["CertificateImagePath"];
string ImgBotPath =ConfigurationManager.AppSettings["CertificateImagePath"];
FileInfo newExistFile = new FileInfo(Certpath);
if (newExistFile.Exists)
{
newExistFile.Delete();
}
PdfWriter oPdfWriter = PdfWriter.GetInstance(doc,new FileStream(Certpath , FileMode.CreateNew));
HTMLWorker htmlWorker = new HTMLWorker(doc);
doc.Open();
GeneratePdfVM data = new GeneratePdfVM();
foreach (var item in ideaidlist)
{
data = new CommonBL().GetIdeaidListForGenerateCertificates(item);
doc.NewPage();
PdfPTable table = new PdfPTable(1);
table.TotalWidth = 1000;
table.WidthPercentage = 100;
table.LockedWidth = true;
table.HorizontalAlignment = 0;
table.DefaultCell.Border = Rectangle.NO_BORDER;
iTextSharp.text.Image imageTopURL = iTextSharp.text.Image.GetInstance(ImgTopPath + "CertiTop.PNG");
PdfPCell imgTopCell = new PdfPCell(imageTopURL);
imgTopCell.Border = Rectangle.NO_BORDER;
table.AddCell(imgTopCell);
imageTopURL.SpacingAfter = 20;
PdfPCell FirstTxtCell = new PdfPCell();
Paragraph p = new Paragraph(data.EmpName);
p.Font = new Font(Font.FontFamily.HELVETICA, 35f, Font.UNDERLINE);
p.Alignment = Element.ALIGN_CENTER;
FirstTxtCell.AddElement(p);
FirstTxtCell.PaddingRight = 190f;
FirstTxtCell.Border = 0;
table.AddCell(FirstTxtCell);
iTextSharp.text.Image imageMidURL = iTextSharp.text.Image.GetInstance(ImgMidPath + "CertiMid.PNG");
PdfPCell imgMidCell = new PdfPCell(imageMidURL);
imgMidCell.Border = Rectangle.NO_BORDER;
imgMidCell.Border = 0;
imageMidURL.SpacingBefore = 15f;
imageMidURL.Alignment = Element.ALIGN_CENTER;
imgMidCell.PaddingRight = 244f;
table.AddCell(imgMidCell);
PdfPCell SecTextCell = new PdfPCell();
Paragraph para = new Paragraph(data.Title);
para.Font = new Font(Font.FontFamily.HELVETICA, 32f, Font.ITALIC);
para.Alignment = Element.ALIGN_CENTER;
SecTextCell.AddElement(para);
SecTextCell.Border = 0;
SecTextCell.PaddingRight = 200f;
table.AddCell(SecTextCell);
iTextSharp.text.Image imageBotURL = iTextSharp.text.Image.GetInstance(ImgBotPath + "CertiBottom.PNG");
PdfPCell imgBotCell = new PdfPCell(imageBotURL);
imgBotCell.Border = 0;
table.AddCell(imgBotCell);
imageBotURL.SpacingBefore=20;
imageTopURL.ScaleAbsolute(860f, 230f);
imageMidURL.ScaleAbsolute(930f, 100f);
imageBotURL.ScaleAbsolute(864f, 230f);
doc.Open();
doc.Add(table);
htmlWorker.StartDocument();
htmlWorker.Parse(txtReader);
htmlWorker.EndDocument();
}
htmlWorker.Close();
doc.Close();
bPDF = ms.ToArray();
ms.Close();
return bPDF;
}
Here I am calling the above function :
public void GenerateCertificatePDF(List<int> ideaidlist)
{
string HTMLContent = "";
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=" + "Certificates.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.BinaryWrite(GetPDF(HTMLContent, ideaidlist));
}
When you run your code locally, a file is created:
new FileStream(Certpath , FileMode.CreateNew)
That same file is created on the server when you run the code on the server.
However, you also want to send the bytes of the PDF document to the browser. To achieve this by creating a MemoryStream:
MemoryStream ms = new MemoryStream();
When I search your code for the ms variable, I don't find it anywhere except at the very end:
bPDF = ms.ToArray();
ms.Close();
return bPDF;
In other words: you don't write any bytes to ms; the MemoryStream is empty. This is proven by the fact that you get 0 bytes.
Your code works in the sense that a PDF is written on the disk of the server, but that's not what you want, is it? You want this method to create a PDF in memory and then send its bytes to a server.
To achieve this, you need to remove all references to certPath, the part where you exist if the file exists and the FileStream. Instead, you need to write the PDF to the MemoryStream:
PdfWriter oPdfWriter = PdfWriter.GetInstance(doc, ms);
This is explained by Chris Haas in his answer to this question: iTextSharp - Create new document as Byte[]
I'm write a generic upload so I can upload items via the API but not having much success. Getting either a 500 unauthorized or 400 bad request. Here's my attempt after a bit of googling. Not sure if this is over complicated. Have also tried a cut down approach. There's a few posts but none that actually work or are confirmed as working. I'll post a simplified version later and I'm aware this will not return a value. Just trying to get passed the exception before I refine.
public JToken DoUpload(string path, string file, params string[] parameters)
{
string result = string.Empty;
if (!path.StartsWith("/"))
{
path = "/" + path;
}
string fullUrl = url + path;// +ToQueryString(parameters);
string contentType = "multipart/form-data";
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
byte[] boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n");
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
wr.ContentType = "multipart/form-data; boundary=" + boundary;
wr.Method = "POST";
//wr.Headers.Add("sessionID","XXXXXXXXXXXXXX"); NEEDED?
Stream rs = wr.GetRequestStream();
rs.Write(boundarybytes, 0, boundarybytes.Length);
string formdataTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
string formitem = string.Format(formdataTemplate, "uploadedFile", "temp.jpg", contentType);
byte[] formitembytes = System.Text.Encoding.UTF8.GetBytes(formitem);
rs.Write(formitembytes, 0, formitembytes.Length);
rs.Write(boundarybytes, 0, boundarybytes.Length);
string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\nContent-Type: {2}\r\n\r\n";
string header = string.Format(headerTemplate, "uploadedFile", "temp.jpg", contentType);
byte[] headerbytes = System.Text.Encoding.UTF8.GetBytes(header);
rs.Write(headerbytes, 0, headerbytes.Length);
FileStream fileStream = new FileStream(file, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[4096];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
rs.Write(buffer, 0, bytesRead);
}
fileStream.Close();
byte[] trailer = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
rs.Write(trailer, 0, trailer.Length);
rs.Close();
WebResponse wresp = null;
Stream stream2;
StreamReader reader2;
try
{
wresp = wr.GetResponse();
stream2 = wresp.GetResponseStream();
reader2 = new StreamReader(stream2);
}
catch (Exception ex)
{
if (wresp != null)
{
wresp.Close();
wresp = null;
}
}
finally
{
wr = null;
}
return result;
}
Figured out a simpler solution if helps anyone:
public JToken DoUpload(string path, string file, string sessionID){
if (!path.StartsWith("/")){
path = "/" + path;
}
string fullUrl = url + path;
string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(fullUrl);
webrequest.ContentType = "multipart/form-data; boundary=" + boundary;
webrequest.Method = "POST";
webrequest.Accept = "application/json;charset=UTF-8";
webrequest.Headers.Add("sessionID", sessionID);
// Build up the post message header
StringBuilder sb = new StringBuilder();
sb.Append("--");
sb.Append(boundary);
sb.Append("\r\n");
sb.Append("Content-Disposition: form-data; name=\"");
sb.Append("uploadedFile");
sb.Append("\"; filename=\"");
sb.Append(Path.GetFileName(file));
sb.Append("\"");
sb.Append("\r\n");
sb.Append("Content-Type: ");
sb.Append("application/octet-stream");
sb.Append("\r\n");
sb.Append("\r\n");
string postHeader = sb.ToString();
byte[] postHeaderBytes = Encoding.UTF8.GetBytes(postHeader);
// Build the trailing boundary string as a byte array
// ensuring the boundary appears on a line by itself
byte[] boundaryBytes = Encoding.ASCII.GetBytes("\r\n--" + boundary + "--\r\n");
FileStream fileStream = new FileStream(file,FileMode.Open, FileAccess.Read);
long length = postHeaderBytes.Length + fileStream.Length + boundaryBytes.Length;
webrequest.ContentLength = length;
Stream requestStream = webrequest.GetRequestStream();
// Write out our post header
requestStream.Write(postHeaderBytes, 0, postHeaderBytes.Length);
// Write out the file contents
byte[] buffer = new Byte[checked((uint)Math.Min(4096, (int)fileStream.Length))];
int bytesRead = 0;
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) != 0)
{
requestStream.Write(buffer, 0, bytesRead);
}
// Write out the trailing boundary
requestStream.Write(boundaryBytes, 0, boundaryBytes.Length);
using (WebResponse response = webrequest.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
return ReadResponse(responseStream);
}
}
}
I have a Windows service that periodically uploads files to an FTP server, and will then redownload the file to check that the bytes match exactly. For most files it seems ok, but one of the files we're using recently is a 10mb CSV file and the files are always slightly different (by a couple of bytes).
Below are the upload, download and file compare methods. Note that the Download is setting UseBinary as true but the upload isn't - could this be why?
public string Upload(FileInfo fi, string targetFilename)
{
//copy the file specified to target file: target file can be full path or just filename (uses current dir)
//1. check target
string target;
if (targetFilename.Trim() == "")
{
//Blank target: use source filename & current dir
target = this.CurrentDirectory + fi.Name;
}
else if (targetFilename.Contains("/"))
{
//If contains / treat as a full path
target = AdjustDir(targetFilename);
}
else
{
//otherwise treat as filename only, use current directory
target = CurrentDirectory + targetFilename;
}
string URI = Hostname + target;
//perform copy
System.Net.FtpWebRequest ftp = GetRequest(URI);
//Set request to upload a file in binary
ftp.Method = System.Net.WebRequestMethods.Ftp.UploadFile;
ftp.UseBinary = false;
//Notify FTP of the expected size
ftp.ContentLength = fi.Length;
//create byte array to store: ensure at least 1 byte!
const int BufferSize = 2048;
byte[] content = new byte[BufferSize - 1 + 1];
int dataRead;
string result = null;
//open file for reading
using (FileStream fs = fi.OpenRead())
{
try
{
//open request to send
using (Stream rs = ftp.GetRequestStream())
{
do
{
dataRead = fs.Read(content, 0, BufferSize);
rs.Write(content, 0, dataRead);
} while (!(dataRead < BufferSize));
rs.Close();
}
}
catch (Exception x)
{
result = URI + " - " + x.ToString();
}
finally
{
//ensure file closed
fs.Close();
}
}
ftp = null;
return result;
}
#endregion
public bool Download(string sourceFilename, FileInfo targetFI, bool PermitOverwrite)
{
//1. check target
if (targetFI.Exists && !(PermitOverwrite))
{
throw (new ApplicationException("Target file already exists"));
}
//2. check source
string target;
if (sourceFilename.Trim() == "")
{
throw (new ApplicationException("File not specified"));
}
else if (sourceFilename.Contains("/"))
{
//treat as a full path
target = AdjustDir(sourceFilename);
}
else
{
//treat as filename only, use current directory
target = CurrentDirectory + sourceFilename;
}
string URI = Hostname + target;
//3. perform copy
System.Net.FtpWebRequest ftp = GetRequest(URI);
//Set request to download a file in binary mode
ftp.Method = System.Net.WebRequestMethods.Ftp.DownloadFile;
ftp.UseBinary = true;
ftp.UsePassive = false;
//open request and get response stream
using (FtpWebResponse response = (FtpWebResponse)ftp.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
//loop to read & write to file
using (FileStream fs = targetFI.OpenWrite())
{
try
{
byte[] buffer = new byte[2048];
int read = 0;
do
{
read = responseStream.Read(buffer, 0, buffer.Length);
fs.Write(buffer, 0, read);
} while (!(read == 0));
responseStream.Close();
fs.Flush();
fs.Close();
}
catch (Exception)
{
//catch error and delete file only partially downloaded
fs.Close();
//delete target file as it's incomplete
targetFI.Delete();
throw;
}
}
responseStream.Close();
}
response.Close();
}
return true;
}
Public Function FileCompare(ByVal file1 As String, ByVal file2 As String) As Boolean
' Checks to see if two files are the same
Dim file1byte As Integer
Dim file2byte As Integer
Dim fs1 As FileStream
Dim fs2 As FileStream
If file1 = file2 Then
Return True
End If
fs1 = New FileStream(file1, FileMode.Open)
fs2 = New FileStream(file2, FileMode.Open)
' Simple length test
If fs1.Length <> fs2.Length Then
fs1.Close()
fs2.Close()
Return False
End If
Do
file1byte = fs1.ReadByte()
file2byte = fs2.ReadByte()
Loop While file1byte = file2byte And file1byte <> -1
fs1.Close()
fs2.Close()
Return ((file1byte - file2byte) = 0)
End Function
I recently tried Dropnet API to connect dropbox app in my C# project. Everything works fine, but I want to upload large files through chunkupload request.
public void FileUpload()
{
string file = #"E:\threading.pdf";
int chunkSize = 1 * 1024 * 1024;
var buffer = new byte[chunkSize];
int bytesRead;
int chunkCount = 0;
ChunkedUpload chunkupload = null;
using (var fileStream = new FileStream(file, FileMode.Open, FileAccess.Read))
{
while ((bytesRead = fileStream.Read(buffer, 0, buffer.Length)) > 0)
{
chunkCount++;
if (chunkCount == 1)
{
chunkupload = client.StartChunkedUpload(buffer);
}
else
{
chunkupload = client.AppendChunkedUpload(chunkupload, buffer);
}
}
}
var metadata = client.CommitChunkedUpload(chunkupload, "/threading.pdf", true);
}
The file size is 1.6 MB. When i Checked, first chunk contains 1 MB and second one contains 0.6MB but only 13 bytes data gets uploaded in each chunk. Could anyone point out problem here.
Update RestSharp to 104.4.0 to resolve this issue.
There's a problem with RestSharper that is used by Dropnet.
Each uploaded chunk uploads exactly 13 bytes
'System.Byte[]'
The problem is that array of bytes is converted to string using method 'AddParameter'.
I didn't dig too much. I'm trying to use UploadFile method.
I have a REST service that returns large files via a Stream as the return type. However I need a way to know how much of the file was transferred even if the download is canceled by the client. What would be the best way to accomplish something like this?
So far I've come up with the following:
public Message GetFile(string path)
{
UsageLog log = new UsageLog();
return WebOperationContext.Current.CreateStreamResponse((stream) =>
{
using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[_BufferSize];
int bytesRead = 0;
long totalBytesRead = 0;
while (true)
{
bytesRead = fs.Read(buffer, 0, _BufferSize);
if (bytesRead == 0)
{
break;
}
try
{
stream.Write(buffer, 0, bytesRead);
}
catch (CommunicationException ex)
{
break;
}
totalBytesRead += bytesRead;
}
log.TransferCompletedUtc = DateTime.UtcNow;
log.BytesTransferred = totalBytesRead;
}
},
"application/octet-stream");
}
I'm interested in hearing any other solutions to accomplishing something like this.