I have some problem. I'am try to save *.docx fail in sql server and show it in web-site. I can save my file in sql server using this code
Byte[] bytes = File.ReadAllBytes(varFilePath);
String file = Convert.ToBase64String(bytes);
using (SqlConnection sql_connetion = new SqlConnection(ConfigurationManager.ConnectionStrings["Database_connection"].ConnectionString))
{
sql_connetion.Open();
using (var sqlWrite = new SqlCommand("INSERT INTO use_of_rule (ID, taj) Values('3', #File)", sql_connetion))
{
sqlWrite.Parameters.Add("#File", bytes);
sqlWrite.ExecuteNonQuery();
return "ok";
}
}
I want to display the content of the file in the web browser. How I can do it?
If you are using Word from WAS (Office Web App Server), you need to do something like this:
1 Create a simple web site to read the data from sql.
You could create a Generic Handler (ashx) in ASP.Net site, providing the plain word file data as response. Dont forget to set the content type as "application/vnd.openxmlformats-officedocument.wordprocessingml.document".
2 Use url encoding to encode the link created in step 1, including all parameters you may want to pass into it.
3 To launch Word, you need to nav it to http(s)://your.was.server.name/op/view.aspx?src=paste.encoded.url.here
Related
I am trying to figure out how to allow my users to be able to save their outlook files to a sql database using c#/.net. I am using a normal file upload control and what I thought was I could just save it is a string but and put in a link button but that didn't work for me.
This was my idea for the code:
string ImageName1 = string.Empty;
byte[] Image1 = null;
if (Images1.PostedFile != null && Images1.PostedFile.FileName != "")
{
ImageName1 = Path.GetFileName(Images1.FileName);
Image1 = new byte[Images1.PostedFile.ContentLength];
HttpPostedFile UploadedImage = Images1.PostedFile;
Images1.PostedFile.InputStream.Read(Image1, 0, Images1.PostedFile.ContentLength);
UploadedImage.InputStream.Read(Image1, 0, (int)Images1.PostedFile.ContentLength);
}
Any information on how to do that would be helpful!
You can store files as BLOBs in the database.
I don't quite follow your code, normally you call a stored procedure and pass parameters or specify the command as text in c# for communicating with the SQL server.
This link gives an example of how to insert blob into database.
In my MVC Application I added a Silverlight Audio Recorder. This recorder record audio using System.Windows.Media; namespace as byte[] that represents an wav file.
Now I need to save this byte[] as .wav file on local disk (server).
So far I've tried the following:
byte[] audioData; // Here I contain my data
System.IO.File.WriteAllBytes(#"C:\Temp\yourfile.wav", this.audioData); // Doesn't work
BinaryWriter Writer = null;
string name = #"C:\Temp\yourfile.wav";
try
{
Writer = new BinaryWriter(File.OpenWrite(name));
Writer.Write(this.audioData);
Writer.Flush();
Writer.Close();
}
catch
{
...
}
But nothing work for me... What I did wrong?
Ok. Let's say I can send my data as json string to the controller:
WebClient net = new WebClient();
string data = UTF8Encoding.UTF8.GetString(this.audioData, 0, this.audioData.Length);
net.UploadStringAsync(new Uri("/Home/SaveFile", UriKind.Relative), data);
How would I get this data in MVC Action?
Silverlight is running client side, your MVC code is server-side, so this won't work in it's current guise.
I'd recommend using a web service to send the data back to your server to save it.
Take a look at using WCF or maybe WebAPI to get up and running with it.
If you really wanted to do get intricate, you could also use something like SignalR to have the client / server (which is essentially what this set up is) send communicate with each other and send the data back through that. Though it seems a little overkill for this.
i deployed a website on IIS running on localhost/xxx/xxx.aspx . On my WPF side , i download a textfile using webclient from the localhost server and save it at my wpf app folder
this is how i do it :
protected void DownloadData(string strFileUrlToDownload)
{
WebClient client = new WebClient();
byte[] myDataBuffer = client.DownloadData(strFileUrlToDownload);
MemoryStream storeStream = new MemoryStream();
storeStream.SetLength(myDataBuffer.Length);
storeStream.Write(myDataBuffer, 0 , (int)storeStream.Length);
storeStream.Flush();
string currentpath = System.IO.Directory.GetCurrentDirectory() + #"\Folder";
using (FileStream file = new FileStream(currentpath, FileMode.Create, System.IO.FileAccess.ReadWrite))
{
byte[] bytes = new byte[storeStream.Length];
storeStream.Read(bytes, 0, (int)storeStream.Length);
file.Write(myDataBuffer, 0, (int)storeStream.Length);
storeStream.Close();
}
//The below Getstring method to get data in raw format and manipulate it as per requirement
string download = Encoding.ASCII.GetString(myDataBuffer);
}
This is by writing btyes and saving them . But how do i download multiple image files and save it on my WPF app folder? I have a URL like this localhost/websitename/folder/designs/ , under this URL , there is many images , how do i download all of them ? and save it on WPF app folder?
Basically i want to download the contents of the folder whereby the contents are actually images.
First, the WebClient class already has a method for this. Use something like client.DownloadFile(remoteUrl, localFilePath).
See this link:
WebClient.DownloadFile Method (String, String)
Secondly, you will need to index the files you want to download on the server somehow. You can't just get a directory listing over HTTP and then loop through it. The web server will need to be configured to enable directory listing, or you will need a page to generate a directory listing. Then you will need to download the results of that page as a string using WebClient.DownloadString and parse it. A simple solution would be an aspx page that outputs a plaintext list of files in the directory you want to download.
Finally, in the code you posted you're saving every single file you download as a file named "Folder". You need to generate a unique filename for each file you want to download. When you're looping through the files you want to download, use something like:
string localFilePath = Path.Combine("MyDownloadFolder", imageName);
where imageName is a unique filename (with file extension) for that file.
I have a unity C# program which is uploading a binary file (with some data).
(which is kind of irrelvant but maybe not)
var form = new WWWForm();
form.AddField("docid", "A");
byte[] textarr = Encoding.ASCII.GetBytes("just a sample text to be compressed and sent to server");
form.AddBinaryData("file", textarr,"file.tmp");
string req = "my url";
WWW www = new WWW(req,form);
I want to take the file content (as binary) so that I will be able to send into the database as binari in the PHP side.
I am trying to do something like this:
$binaridata = ~$_FILES["file"]["name"] (what do i need to do here? tried
file_get_content etc, but it always yield some errors)
Thanks for your help!
$binaryData = file_get_contents($_FILES['file']['tmp_name']);
^^^^^^^^
The file is stored at the path indicated by tmp_name, name is just the name given by the client and practically irrelevant.
Depending on how you talk to the database and what database, you don't want to read the entire file contents into a variable though. For instance, using Postgres via PDO, you'd do this:
$stmt = $pdo->prepare('INSERT INTO ... VALUES (:file)');
$fh = fopen($_FILES['file']['tmp_name'], 'rb');
$stmt->bindParam(':file', $fh, PDO::PARAM_LOB);
$stmt->execute();
The database adapter will read the file as a stream, which is much more economic than saving it in memory. Consult your database adapter's manual.
why not try
$binaridata = $_FILES["file"]["name"];
add the semicolon and remove the ~
Shadowpat
I have a html to pdf conversion tool which resides on our server that I can access using a url with querystrings.
Something like myurl.com/createpdf.aspx?page=http://www.somesite.com.
The tool then converts the web page and displays it as a pdf.
I would like to offer this functionality as a web service, so that our clients can access this programmatically.
Ideal case would be, that our clients send in the site they would like to have converted and the web service then returns the pdf.
Would that make sense?
What would be the best way to implement this?
cheers,
Terry
Use file IO to read the PDF in and have your webmethod return it as a byte[] array. For example like this:byte[] filebytes = null;
using (FileStream fs = File.OpenRead(fileName))
{
filebytes = new byte[fs.Length];
fs.Read(filebytes, (int)0, (int)fs.Length);
}
return filebytes;
It will be Base64 encoded and you will then be able to Save and view the file on the client.
You could also write an http-handler that returns the pdf with the appropriate mime type. This way you would not have all the processing overhead of an aspx page.