I am using websockets to send a file from my c# server to my browser. I added the file name and filebytelength along with the file in the bytestream.
My c# code to include file name in the byte stream
string filename = "#\C:\Users\Username\Downloads\RAW1MB data.txt";
string shortName = System.IO.Path.GetFileName(FileName);
byte[] fileNameByte = Encoding.UTF8.GetBytes(shortName);
byte[] fileNamelen = BitConverter.GetBytes(shortName.Length);
byte[] filedata = File.ReadAllBytes(FileName);
byte[] senddata = new byte[4 + fileNameByte.Length + filedata.Length];
fileNamelen.CopyTo(senddata, 0);
fileNameByte.CopyTo(senddata, 4);
filedata.CopyTo(senddata, 4 + fileNameByte.Length);
await webSocket.SendAsync(new ArraySegment<byte>(senddata,0,senddata.Length),WebSocketMessageType.Binary,true,CancellationToken.None );
This is my Javascript included in my HTML
websocket.onmessage = function (e) {
if(e.data instanceof Blob){
window.alert("Getting file");
var filedata = e.data;
var filenameLen = filedata.slice(0,4);
Console.log(filenamelen);// I am getting Blob{ size: 4 ,type: ""}
}
else{
writeToScreen("<span>RESPONSE: " + e.data + "</span>");
}
};
From the console.log I am getting Blob{ size:4,type :""}.
How do I get the integer and string from first 4 bytes and the following respectively?
So without getting the filenamelen I am unable to get the filename as well.
Related
I need to download every file of type js and c#.
This is my api code:
[HttpPost]
public HttpResponseMessage GetFile(DownloadInput input)
{
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);
var fileNm = input.FileName;
string filePath = (#"C:\Uploads\" + input.ID + #"\" + fileNm);
if (!File.Exists(filePath))
{
response.StatusCode = HttpStatusCode.NotFound;
response.ReasonPhrase = string.Format("File not found: {0} .", fileNm);
throw new HttpResponseException(response);
}
byte[] bytes = File.ReadAllBytes(filePath);
response.Content = new ByteArrayContent(bytes);
response.Content.Headers.ContentLength = bytes.LongLength;
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = fileNm;
response.Content.Headers.ContentType = new MediaTypeHeaderValue(MimeMapping.GetMimeMapping(filePath));
return response;
}
I think ı am rong this part this code download every file but just work .txt type file, I think blob type is false but ı am new this subject ı am tried every code ,
This is my js code:
function FileDown(response, name) {
let blob = new Blob([response], { type: "application/octet-stream" });
let link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = name;
link.click();
}
You can try the following, I've used it successfully many times.
First you need to make sure the data you save is correct. Since you need the mimetype to successfully download files without complications.
Here is the C# (PS: You can make your Endpoint HttpGet, instead of HttpPost.)
[HttpGet]
[Route("YourController/{fileName}")]
public HttpResponseMessage Download(string fileName) //Parameter is yours
{
string filePath = (#"C:\Uploads\" + input.ID + #"\" + fileName);
if (!File.Exists(filePath))
{
response.StatusCode = HttpStatusCode.NotFound;
response.ReasonPhrase = string.Format("File not found: {0} .", fileNm);
throw new HttpResponseException(response);
}
byte[] bytes = File.ReadAllBytes(filePath);
using (var ms = new MemoryStream(bytes))
{
var result = new HttpResponseMessage(HttpStatusCode.OK)
{
Content = new ByteArrayContent(ms.ToArray())
};
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = fileName //String value of the file name.
};
string mimeType = MimeMapping.GetMimeMapping(fileName); //I've found that this does not always work. Go here for a better answer: https://stackoverflow.com/questions/1029740/get-mime-type-from-filename-extension
result.Content.Headers.ContentType = new MediaTypeHeaderValue(mimeType); //The mime type retrieved from the
return result;
}
}
In javascript you can use: window.open('yourApiURL/YourController/FileName');
Using the sapui5 uploadcollection to upload files in the frontend and then sending them through ajax with a post request...
I need to know how to convert te returned object from the uploadcollection control into a xstring, so then I can send that xstring (that contains the file content) To a sap gateway by using ajax post method.
Any idea how could I do this?
Right now I'm sending files by using the uploadcollection, once I upload an attachment, the control returns an object that represents the file content.
I'm trying to make this object a xstring by using filereader:
//obtiene archivo
var file = files[i];
//Convierte archivo en binario
var reader = new FileReader();
reader.onload = function(readerEvt) {
var binaryString = readerEvt.target.result;
var base64 = btoa(binaryString);
var base64file;
if(typeof base64file == "undefined" || typeof base64file == null){
base64file = base64;
}else{
base64file = base64file +'new'+base64;
}
};
reader.readAsBinaryString(file);
console.log(file)
But this work only with files of type image, the others like pdf, .doc etc etc give the following error when I try to send them with ajax.
"The Data Services Request could not be understood due to malformed syntax".
Any idea how can I send convert these files into a xstring data?
Take a look at this example. Hope this helps.
View
<u:FileUploader change="onChange" fileType="pdf" mimeType="pdf" buttonText="Upload" />
Controller
convertBinaryToHex: function(buffer) {
return Array.prototype.map.call(new Uint8Array(buffer), function(x) {
return ("00" + x.toString(16)).slice(-2);
}).join("");
},
onChange: function(oEvent){
var that = this;
var reader = new FileReader();
var file = oEvent.getParameter("files")[0];
reader.onload = function(e) {
var raw = e.target.result;
var hexString = that.convertBinaryToHex(raw).toUpperCase();
// DO YOUR THING HERE
};
reader.onerror = function() {
sap.m.MessageToast.show("Error occured when uploading file");
};
reader.readAsArrayBuffer(file);
},
I figured it out by filling an array everytime that a file was uploaded through the control,
change: function(oEvent) {
//Get file content
file = oEvent.getParameter("files")[0];
//Prepare data for slug
fixname = file.name;
filename = fixname.substring(0, fixname.indexOf("."));
extension = fixname.substring(fixname.indexOf(".") + 1);
//fill array with uploaded file
var fileData = {
file: file,
filename: filename,
extension: extension
}
fileArray.push(fileData);
},
and then I did a loop over that array to post every single file I keept there by using ajax method post.
$.each(fileArray, function(j, valor) {
//get file
file = fileArray[j].file;
//get file lenght
var numfiles = fileArray.length;
//Convert file to binary
var reader = new FileReader();
reader.readAsArrayBuffer(file);
reader.onload = function(evt) {
fileString = evt.target.result;
//get and make slug
filename = fileArray[j].filename;
extension = fileArray[j].extension;
slug = documento + '/' + filename + '/' + extension;
//User url service
var sUrlUpload = "sap url";
runs++;
//Post files
jQuery.ajax({});
}
});
I am trying to convert an image to base64 to upload it on sharepoint site but it is throwing 400:bad request error. when i checked properly then i found out that the base64 i am sending is endcoded by javascript and it is different than what is expected by sharepoint. I have attached 2 images here describing the difference. Can anyone help me to get the proper encoded data using javascript ?
javascript encoded base64
c# encoded base64
var files = $("#myfile").get(0).files;
var reader = new FileReader();
reader.readAsDataURL(files[0]);
reader.onload = function () {
console.log(reader.result);
}
Could try : reader.result.split("base64,")[1]
Removes the "base64," start of the string.
Please try this , i am using this in my project , its working for me
if (file.ContentType.Contains("image"))
{
string theFileName = Path.GetFileName(file.FileName);
byte[] thePictureAsBytes = new byte[file.ContentLength];
using (BinaryReader theReader = new BinaryReader(file.InputStream))
{
thePictureAsBytes = theReader.ReadBytes(file.ContentLength);
}
string thePictureDataAsString = Convert.ToBase64String(thePictureAsBytes);
}
"thePictureDataAsString " variable got Base64 string
.........................................................................
i am getting file like this in my project
public ActionResult SaveMake(string inputMakeName, HttpPostedFileBase file)
{
MakeModel objMakeModel = new MakeModel();
if (file.ContentType.Contains("image"))
{
string theFileName = Path.GetFileName(file.FileName);
byte[] thePictureAsBytes = new byte[file.ContentLength];
using (BinaryReader theReader = new BinaryReader(file.InputStream))
{
thePictureAsBytes = theReader.ReadBytes(file.ContentLength);
}
string thePictureDataAsString = Convert.ToBase64String(thePictureAsBytes);
objMakeModel.ImageBase64 = thePictureDataAsString;
objMakeModel.Make1 = inputMakeName;
}
string response = _apiHelper.ConvertIntoReturnStringPostRequest<MakeModel>(objMakeModel, "api/Transaction/SaveMakes/");
// string response = _apiHelper.SaveMake(objMakeModel, "api/Transaction/SaveMakes/");
return RedirectToAction("AddVehicleMaintenance");
}
In our mobile application (cordova+html4) we have a requirement to display the PDF from a stream. We have a service which returns pdf stream. We would like to store that stream to a temp folder location of the mobile and display the PDF.
The below sample java sample java code does the exact thing what I need. But how can I achive this functionality on java script? I mean reading a binary stream in java script.
String fileURL = "https://1/////4/xyz";
String saveDir = "D:/Works";
try {
URL url = new URL(fileURL);
HttpURLConnection httpConn = (HttpURLConnection) url
.openConnection();
httpConn.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
httpConn.setRequestProperty("TOKEN",
"ghZtxnPfpJ63FgdT/59V+5zFTKHRdwm6rIfGJC+0B5W5CJ9pG33od7l+/L6S8R56");
int responseCode = httpConn.getResponseCode();
System.out.println("Reseponse Code = " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK) {
String fileName = "";
String disposition = httpConn
.getHeaderField("Content-Disposition");
String contentType = httpConn.getContentType();
int contentLength = httpConn.getContentLength();
if (disposition != null) {
// extracts file name from header field
int index = disposition.indexOf("filename=");
if (index > 0) {
fileName = disposition.substring(index + 10,
disposition.length() - 1);
}
} else {
// extracts file name from URL
fileName = fileURL.substring(fileURL.lastIndexOf("/") + 1,
fileURL.length());
}
System.out.println("Content-Type = " + contentType);
System.out.println("Content-Disposition = " + disposition);
System.out.println("Content-Length = " + contentLength);
System.out.println("fileName = " + fileName);
// opens input stream from the HTTP connection
InputStream inputStream = httpConn.getInputStream();
String saveFilePath = saveDir + File.separator + fileName;
// opens an output stream to save into file
FileOutputStream outputStream = new FileOutputStream(
saveFilePath);
int bytesRead = -1;
byte[] buffer = new byte[BUFFER_SIZE];
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
inputStream.close();
System.out.println("File downloaded");
} else {
System.out
.println("No file to download. Server replied HTTP code: "
+ responseCode);
}
httpConn.disconnect();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Thre's two libraries that you may want to have a look at :
jsPDF if you're trying to generate pdf from javascript and
pdf.js if you're trying to implement a pdf viewer in your interface.
One of those two should do the trick for you.
In my JSP, I have a button named "Download Zip file". When I click the button, I want to fetch data from the database and write it into a JS file and keep it inside a folder and download folder in ZIP format. I am using struts2.
How can I do this?
One way is to serve binary data from a servlet. Something like this:
byte[] zipFileBytes = ...;// generate the zip file and get the bytes
response.setContentType("application/octet-stream");
response.getOutputStream().write(zipFileBytes );
Then use a standard anchor element to download the file:
<a src="url to your servlet">download the file</a>
You might need to play with this a little bit to match your exact use case.
Try this : code to download file as a Zip
ServletOutputStream servletOS = null;
String zipFileName = null;
try {
servletOS = response.getOutputStream();
final ResourceResolver resolver = request.getResourceResolver();
zipFileName = FileDownloadHelper.getDownloadZipFileName();
response.setContentType("application/zip");
response.addHeader("Content-Disposition", "attachment; filename=" + zipFileName);
servletOS.write(FileDownloadHelper.prepareZipDownloadOutputStream(servletOS, documentUrls));
} finally {
if (servletOS != null) {
servletOS.flush();
servletOS.close();
}
}
public static byte[] prepareZipDownloadOutputStream(final ServletOutputStream outputStream,
final List<String> docUrls) throws IOException {
final byte[] buf = new byte[2048];
String fileName;
ZipOutputStream zipOutputStream = null;
InputStream isInputStream = null;
try {
zipOutputStream = new ZipOutputStream(outputStream);
for (final String docUrl : docUrls) {
LOGGER.info("Reading file from DAM : " + docUrl);
// read this file as input stream
isInputStream = new FileInputStream(docUrl);
if (isInputStream != null) {
fileName = getFileNameFromDocumentUrl(docUrl);
// Add ZIP entry to output stream.
zipOutputStream.putNextEntry(new ZipEntry(fileName));
int bytesRead;
while ((bytesRead = isInputStream.read(buf)) != -1) {
zipOutputStream.write(buf, 0, bytesRead);
}
zipOutputStream.closeEntry();
} e
}
} finally {
if (zipOutputStream != null) {
zipOutputStream.flush();
zipOutputStream.close();
}
if (isInputStream != null) {
isInputStream.close();
}
}
LOGGER.info("Returning buffer to be written to response output stream");
return buf;
}
public static String getFileNameFromDocumentUrl(final String docUrl) {
return docUrl
.substring(docUrl.lastIndexOf("/") + 1, docUrl.length());
}