Problem in downloading the file using sharepoint copy.asmx - python

I am trying to download the file from the document using sharepoint webservices called copy.asmx. its onlt 100 kb file size.
But its not downloading the file.
The web services iteself return empty stream (out byte[] Stream) in the web service response. is that any memory issue.
Also it returning like "download_document()out of memory"
Note: I am using the MFP printer to view this application.

Please try below function. you need to pass FileURL(Full web url for document), Title(Pass name you want to give for downloaded file.)
public string DownLoadfiletolocal(string FileURL, string Title)
{
//Copy.Copy is a webservice object that I consumed.
Copy.Copy CopyObj = new Copy.Copy();
CopyObj.Url = SiteURL + "/_vti_bin/copy.asmx"; // Dynamically passing SiteURL
NetworkCredential nc2 = new NetworkCredential();
nc2.Domain = string.Empty;
nc2.UserName = _UserName;
nc2.Password = _Password;
string copySource = FileURL; //Pass full url for document.
Copy.FieldInformation myFieldInfo = new Copy.FieldInformation();
Copy.FieldInformation[] myFieldInfoArray = { myFieldInfo };
byte[] myByteArray;
// Call the web service
uint myGetUint = CopyObj.GetItem(copySource, out myFieldInfoArray, out myByteArray);
// Convert into Base64 String
string base64String;
base64String = Convert.ToBase64String(myByteArray, 0, myByteArray.Length);
// Convert to binary array
byte[] binaryData = Convert.FromBase64String(base64String);
// Create a temporary file to write the text of the form to
string tempFileName = Path.GetTempPath() + "\\" + Title;
// Write the file to temp folder
FileStream fs = new FileStream(tempFileName, FileMode.Create, FileAccess.ReadWrite);
fs.Write(binaryData, 0, binaryData.Length);
fs.Close();
return tempFileName;
}

Related

Extract zip file inline in Oracle OCI - Object Storage without downloading to save time

Is it possible to extract a zip file 'inline' which is in cloud say Oracle cloud, Object storage. Meaning, without downloading it and extracting it in the o/s and re-uploading it to object storage, because the file size is big and we need to save time on upload/download.. Any sample code, with Oracle Functions, or python, java etc. ? Is it possible ? I tried with S3 browser/explorer or other similar tools, but that basically at the background, downloads and extract on local computer.
If I understand the question correctly, your use case would be that you have a compressed value on the server and want to extract it on the server and keep it there.
This is possible and mostly depends on how the values has been compressed.
If you use the Lempel-Ziv-Welch algorithm used in the UTL_COMPRESS package, you can extract it directly in PL/SQL.
For other formats like zip, you will need to use some custom Java code like the following example:
CREATE OR REPLACE
JAVA SOURCE NAMED ZIP_Java
AS
import java.io.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import java.util.zip.ZipInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.BufferedOutputStream;
import java.sql.Blob;
public class Java_Class {
public static int ZipBlob(Blob inLob, Blob[] outLob, String filename) {
try {
// create the zipoutputstream from the end of the outLob
Blob zipLob = outLob[0];
OutputStream os = zipLob.setBinaryStream(1);
ZipOutputStream zos = new ZipOutputStream(os);
// add one zip entry
ZipEntry entry = new ZipEntry(filename);
zos.putNextEntry(entry);
// write data to the zip lob
long len = inLob.length();
long offset = 0;
byte[] buffer;
int chunksize = 32768;
while (offset < len) {
buffer = inLob.getBytes(offset + 1, chunksize);
if (buffer == null)
break;
zos.write(buffer, 0, buffer.length);
offset += buffer.length;
}
zos.closeEntry();
zos.close();
outLob[0] = zipLob;
} catch (Exception e) {
System.out.println("Exception: " + e.toString());
e.printStackTrace(System.out);
return 0;
}
return 1;
}
public static int UnzipBlob(Blob inLob, Blob[] outLob, String filename) {
try {
final int kBUFFER = 2048;
InputStream inps = inLob.getBinaryStream();
ZipInputStream zis = new ZipInputStream(inps);
ZipEntry entry;
Blob fileLob = outLob[0];
OutputStream os = fileLob.setBinaryStream(1);
while((entry = zis.getNextEntry()) != null) {
if (entry.getName().equalsIgnoreCase(filename)) {
byte data[] = new byte[kBUFFER];
BufferedOutputStream dest = new BufferedOutputStream(os, kBUFFER);
int count;
while ((count = zis.read(data, 0, kBUFFER)) != -1) {
dest.write(data, 0, count);
}
dest.flush();
dest.close();
}
}
zis.close();
return 1;
} catch (Exception e) {
System.out.println("Exception: " + e.toString());
e.printStackTrace();
return 0;
}
}
}
/
CREATE OR REPLACE
FUNCTION ZipBlobJava(theSource IN BLOB, theDestination IN OUT NOCOPY BLOB, theFilename IN VARCHAR2) RETURN NUMBER
AS LANGUAGE JAVA NAME 'Java_Class.ZipBlob(java.sql.Blob, java.sql.Blob[], java.lang.String) return int';
/
CREATE OR REPLACE
FUNCTION UnzipBlobJava(theSource IN BLOB, theDestination IN OUT NOCOPY BLOB, theFilename IN VARCHAR2) RETURN NUMBER
AS LANGUAGE JAVA NAME 'Java_Class.UnzipBlob(java.sql.Blob, java.sql.Blob[], java.lang.String) return int';
/

Sending Real Time images captured using unity camera

server
private void SendImageByte()
{
image_bytes = cm.Capture();
print(image_bytes.Length);
if (connectedTcpClient == null)
{
return;
}
try
{
// Get a stream object for writing.
NetworkStream stream = connectedTcpClient.GetStream();
if (stream.CanWrite)
{
// string serverMessage = "This is a message from your server.";
// Convert string message to byte array.
byte[] serverMessageAsByteArray = Encoding.ASCII.GetBytes(image_bytes.ToString());
// Write byte array to socketConnection stream.
stream.Write(serverMessageAsByteArray, 0, serverMessageAsByteArray.Length);
Debug.Log("Server sent his message - should be received by client");
}
}
catch (SocketException socketException)
{
Debug.Log("Socket exception: " + socketException);
}
}
client
import socket
host = "127.0.0.1"
port = 1755
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
def receive_image():
data = sock.recv(999999).decode('utf-8')
print(len(data))
while True:
receive_image()
here script capture images from unity camera
public byte[] Capture()
{
if(renderTexture == null)
{
// creates off-screen render texture that can rendered into
rect = new Rect(0, 0, captureWidth, captureHeight);
renderTexture = new RenderTexture(captureWidth, captureHeight, 24);
screenShot = new Texture2D(captureWidth, captureHeight, TextureFormat.RGB24, false);
}
// _camera = GetComponent<Camera>();
_camera.targetTexture = renderTexture;
_camera.Render();
// reset active camera texture and render texture
_camera.targetTexture = null;
RenderTexture.active = null;
// read pixels will read from the currently active render texture so make our offscreen
// render texture active and then read the pixels
RenderTexture.active = renderTexture;
screenShot.ReadPixels(rect, 0, 0);
screenShot.Apply();
byte[] imageBytes = screenShot.EncodeToPNG();
//Object.Destroy(screenShot);
//File.WriteAllBytes(Application.dataPath + "/../"+ imagePath + "/img{counter}.png", bytes);
//counter = counter + 1;
return imageBytes;
}
Am trying to send real-time images on Unity3D from C# to python using socket communication to be processed and return back values to unity, but the problem even the bytes length received on the client is not the same as the server. I send about 400K bytes but I receive only 13
C# is the server and python is the client
or am doing it wrong but the main goal I want to create simulator work as udacity self-driving
Are you sure that image_bytes.ToString() returns what you expect and not maybe just something like "System.Byte[]" => 13 chars => 13 bytes.
In general why would you convert an already byte[] into a string just in order to convert it back into byte[] to send? I'm pretty sure you do not want to transmit binary image data using UTF-8 ... one option might be Base64 string, but still that would be quite inefficient.
Just send the raw bytes like e.g.
stream.Write(image_bytes, 0, image_bytes.Length);
And then receive until you receive that length.
A typical solution is to prepend the length of the message to send and on the receiver side actually wait until you received exactly that amount of bytes like e.g.
var lengthBytes = BitConverter.GetBytes(image_bytes.Length);
stream.Write(lengthBytes, 0, lengthBytes.Length);
stream.Write(image_bytes, 0, image_bytes.Length);
Now you know that on the receiver side you first have to receive exactly 4 bytes (== one int) which will tell you exactly how many bytes to receive for the actual payload.
Now I'm no python expert but after googling around a bit I think something like
def receive_image()
lengthBytes = sock.recv(4)
length = struct.unpack("!i", lengthBytes)[0]
data = sock.recv(length)
Note: After reading John Gordon's comment on the question I guess this still doesn't fully solve the waiting until according buffers are actually filled - as said no python expert - but I hope it gives you a idea where to go ;)

Converting Python struct.pack to C#

I'm converting some python code to C# and was successful so far, but there's this one part that I don't understand and is arguably the most important part as it involves file decompression and turning into JSON and human readable format.
file_data: bytes = file.read() 'I understand this
file_data: bytes = struct.pack('B' * len(file_data), *file_data[::-1]) 'This is the part that I do not understand, everything after the 'B'
file_data: bytes = zlib.decompress(file_data) 'should be fine, c# should have same library
file_data: tuple = pickle.loads(file_data, encoding='windows-1251')
That python code line simply reverses the code and removes the header.
Here's the c# code that does the same.
Hashtable UnpickledGP;
byte[] FileBytes = File.ReadAllBytes("GameParams.data").Reverse().ToArray();
byte[] ModdedFileBytes = new byte[FileBytes.Length - 2]; //remove file header
Array.Copy(FileBytes, 2, ModdedFileBytes, 0, ModdedFileBytes.Length);
using (Stream StreamTemp = new MemoryStream(ModdedFileBytes))
{
using (MemoryStream MemoryStreamTemp = new MemoryStream())
{
using (DeflateStream DeflateStreamTemp = new DeflateStream(StreamTemp, CompressionMode.Decompress))
{
DeflateStreamTemp.CopyTo(MemoryStreamTemp);
}
ModdedFileBytes = MemoryStreamTemp.ToArray();
}
}
using (Unpickler UnpicklerTemp = new Unpickler())
{
object[] UnpickledObjectTemp = (object[])UnpicklerTemp.loads(ModdedFileBytes);
UnpickledGP = (Hashtable)UnpickledObjectTemp[0];
UnpicklerTemp.close();
}

How to send image in C# to a Flask Server that is decoded by OpenCV?

Here is part of my Flask API in Python:
image_data = flask.request.get_data() # image_data's data type
string image_vector = numpy.frombuffer(image_data, dtype=numpy.uint8)
image = cv2.imdecode(image_vector, cv2.IMREAD_COLOR)
How would I send a image that I encoded like below, in C#:
ResultString = "Loading...";
var surface = SKSurface.Create(new SKImageInfo((int)canvasView.CanvasSize.Width,
(int)canvasView.CanvasSize.Height));
var canvas = surface.Canvas;
canvas.Clear();
foreach (SKPath path in completedPaths)
canvas.DrawPath(path, paint);
foreach (SKPath path in inProgressPaths.Values)
canvas.DrawPath(path, paint);
canvas.Flush();
var snap = surface.Snapshot();
var pngImage = snap.Encode(SKEncodedImageFormat.Png, 100);
AnalyerResults analyerResults = mathclient.AnalyzeWork(pngImage);
try { ResultString = analyerResults.message; } catch { ResultString = "Error..."; }
How would I send the image to in C# to be able to be received and decoded like shown in part of my API?
I already tried:
HttpClient client = await GetClient();
var result = await client.PostAsync(Url + "analyzer", new ByteArrayContent(pngImage.ToArray()));
return JsonConvert.DeserializeObject<AnalyerResults>(await result.Content.ReadAsStringAsync());
I also tried:
var client = new RestClient(Url + "analyzer");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "image/png");
request.AddParameter("image/png", pngImage, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
return JsonConvert.DeserializeObject<AnalyerResults>(response.Content);
However in both the content returned null. This question is related to How to Replicate this Postman Request which has a Binary Content Body and contains a .PNG File in C#?.

Extract the text from url which is Pdf

I want to extract the text from this pdf. I cannot do this with pypdf as the document was scanned.
Your pdf that you want to extract text from is actually just a bunch of scanned photos. Since PdfFileReader and other pdf readers extract text based on the metadata of the document you won't get any results with that (If text isn't already embedded in the PDF, then you'll need to use OCR to extract the text.).
You can use Tesseract for that, Tesseract doesn't ocr pdf's so transform .pdf to .tiff with something like convert:
convert -density 300 /path/to/my/document.pdf -depth 8 -strip -background white -alpha off file.tiff
Then use tesseract on that file:
tesseract file.tiff output.txt
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
public class PDFreader {
public static void main(String[] args) throws Exception
{
URL url = new URL("http:/....view.php?fil_Id=5515");
byte[] response = null;
try (InputStream in = new BufferedInputStream(url.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream()) {
byte[] buf = new byte[1024];
int n = 0;
int counter = 0;
while (-1 != (n = in.read(buf))) {
out.write(buf, 0, n);
counter = counter + n;
}
response = out.toByteArray();
}
OutputStream os = new FileOutputStream("abc.pdf");
os.write(response);
os.close();
File file = new File("abc.pdf");
PDDocument document = PDDocument.load(file);
PDFTextStripper pdfStripper = new PDFTextStripper();
String text = pdfStripper.getText(document);
System.out.println(text);
document.close();
}
}

Categories