Using plupload control with .NET - c#

I downloaded the source project from the website, using as is, except I changed the target file from upload.php to upload.aspx, which contains the following code to receive the file data:
int chunk = Request.QueryString["chunk"] != null ? int.Parse(Request.QueryString["chunk"]) : 0;
string fileName = Path.GetFileName(Request.Files[0].FileName);
// Read stream
BinaryReader br = new BinaryReader(Request.InputStream);
byte[] buffer = br.ReadBytes((int)br.BaseStream.Length);
br.Close();
//byte[] appended = buffer.Take(149).ToArray();
// Write stream
BinaryWriter bw = new BinaryWriter(File.Open(Server.MapPath("~/uploadfiles" + fileName), chunk == 0 ? FileMode.Create : FileMode.Append));
bw.Write(buffer);
bw.Close();
The problem is when I upload a jpg file, or any other file, there is data prepended and appended to every chunk, that obviously makes the file corrupted, and increases the file size. Any idea why that would happen?

You ned to read from Request.Files[0] not from Request.InputStream.
see marco's post: here

Related

C# error on reading mp3 file by binary reader : The process cannot access the file 'URL\testbinary.mp3' because it is being used by another process

hiiii,
i want to read mp3 file by using binary reader, my code is :
using (BinaryReader br = new BinaryReader(File.Open("Songs/testbinary.mp3", FileMode.Open)))
{
int length = (int)br.BaseStream.Length;
byte[] bytes = br.ReadBytes(length);
txtBinary.Text = bytes.ToString();
}
.......
when i execute this code it shows and exception:
The process cannot access the file 'URL\testbinary.mp3' because it is being used by another process.
where "URL" is my actual file location.
You open the same file twice (without any sharing option). To read the content of a file as bytes you can use File.ReadAllBytes
byte[] bytes = File.ReadAllBytes("Songs/testbinary.mp3");
BTW: Don't forget txtBinary.Text = bytes.ToString(); doesn't give you what you think. You will have to use BitConverter.ToString or Convert.ToBase64String

PDF Upload error System.IO.DirectoryNotFoundException: Could not find a part of the path

I have developed an ASP.net C# function to upload PDF to the Database. when I try it in LocalHost, its working perfectly fine. but when I publish it in a server on IIS. it gives me the below error when I click upload:
System.IO.DirectoryNotFoundException: Could not find a part of the
path + <path of the file>
string filePath = Path.GetFullPath(FileUpload1.PostedFile.FileName);
string filename = Path.GetFileName(filePath);
FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
br.Close();
fs.Close();
Is there anything I should change in order to be able to upload?
When you access it from localhost, both client and server are same so it can find file. but when you publish both the machine are isolated. basically you are not getting content from uploaded file, what you did is get the filename and fetch data from local hard disk, you should use following snippet.
int fileLen = fu.PostedFile.ContentLength;
Byte[] Input = new Byte[fileLen];
Stream myStream = fu.PostedFile.InputStream;
myStream.Read(Input, 0, Input.Length);
I have declared an byte array with size of bytes in uploaded file. and read byte from PostedFile InputStream.

Can not read the flat file source

I want to get the file name from user and then according to that name i will get another input. It's a multi input multi output problem.
So i Normalized the problem as below.
1.I am using Script Tast in Control flow And asked to enter the file Name Using a form in C#. I am getting file name as browsing the file.
2.Then Unzip the file and check the name of the file according to the name of the file i set 3 varible which are globel.
3.According to variables i took 3 Data flow tasks. And its working well for the above specified problem.
4.Its Working for different Data flow tasks.
But i have another issue in this when i am using the flat file source name as dynamicly(Using Expression) It is giving me error about the Can not open the file.
Error "[Flat File Source [1]] Error: Cannot open the datafile "C:\Documents and Settings\XQN4P\Desktop\Inputs\Intraday\OPTION_DAILY_INTRADAY_ASIA20140212.csv". "
Note this file is generated from Unziping the entered file by user.
There is warning as well "[Flat File Source [1]] Warning: The process cannot access the file because it is being used by another process.
".
here is the code i am using to unzip it.
}
private void unzipInFile(string inputFilePath, string destDirectory,string destFileName)
{
FileStream fsIn =new FileStream (inputFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
FileStream fsOut =new FileStream(destDirectory +"\\"+destFileName, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
int buffersize = 4096;
int count = 0;
var buffer= new byte [buffersize];
//using (Stream compressed = File.OpenRead(inputFilePath))
//using (ZlibStream zlib = new ZlibStream(compressed, CompressionMode.Decompress))
using(GZipStream gZip = new GZipStream(fsIn, Ionic.Zlib.CompressionMode.Decompress, Ionic.Zlib.CompressionLevel.BestCompression, true ))
{
byte[] buf = new byte[short.MaxValue];
int bufl;
while (0 != (bufl = gZip.Read(buf, 0, buf.Length)))
{
if (count != 0)
fsOut.Write(buffer, 0, count);
if (count != buffersize)
return ;
}
}
fsOut.Close();
}
Another Address variable
Dts.Variables["FileAddress"].Value = filePath + ".csv";
SSIS structure.:
"ScriptTask" sending output to 3 Data Flow Tasks.
Problem Solved.
Problem was in formating of the unzipped file.
It was not able to get the row delimiter.
And some times file was still in use.
So i worked around for this and it's ok

How to insert a default image into database when the path is null?

I'm facing one problem which when I want to insert image into the database but the path is null then I will like a default picture to be insert into the default. Can this be possible???
if (imageName != "")
{
MessageBox.Show(imageName);
//Initialize a file stream to read the image file
FileStream fs = new FileStream(#imageName, FileMode.Open, FileAccess.Read);
//Initialize a byte array with size of stream
byte[] imgByteArr = new byte[fs.Length];
//Read data from the file stream and put into the byte array
fs.Read(imgByteArr, 0, Convert.ToInt32(fs.Length));
fs.Close();
cmdInsert.Parameters.AddWithValue("#fImage", imgByteArr);
}
else {
imageName = #"/HouseWivesSavior;component/images/unknown.jpg";
//Initialize a file stream to read the image file
FileStream fs = new FileStream(#imageName, FileMode.Open, FileAccess.Read);
//Initialize a byte array with size of stream
byte[] imgByteArr = new byte[fs.Length];
//Read data from the file stream and put into the byte array
fs.Read(imgByteArr, 0, Convert.ToInt32(fs.Length));
fs.Close();
cmdInsert.Parameters.AddWithValue("#fImage", imgByteArr);
cmdInsert.Parameters.AddWithValue("#fImage", "");
}
Don't do that. Why would you bother keeping a default image in the database? This is expensive in terms of storage and network traffic. Imagine having 1024 records with the same default image of 200KB, you will be wasting 200MB of db storage. Not only that, retrieving BLOBs from a database is a very expensive operation.
Avoid it, if the path is null then insert exactly that, a null value. Then let your business logic decide what to do when there's not image.
And by the way, never check for empty strings like you did in your question. Use the IsNullOrEmpty static method of the string object instead
if(!string.IsNullOrEmpty(imageName))
That will evaluate for null and empty values more efficiently
Hope it helps
Leo
Instead of checking for an empty string, check for null.
var imagePath = string.Empty;
if (imageName == null)
imagePath = "pathToDefaultImage";
else
imagePath = "pathToNonDefaultImage"
... do your work here using imagePath as the path
But you could just save null in your database. And then when you're loading your images from your database, just set a default image when you encounter null. That way you're not saving the same default image over and over.

Read Byte Array from a zip file in project folder in WP7

I have a zip file and I have to read its bytes and decrypt. How do I get the byte array from the file which I have added to the project and set its bulid action property as content. Help me.
You can get the bytes of the file this way:
var res = Application.GetResourceStream(new Uri("yourFile", UriKind.Relative));
var fileStream = res.Stream;
byte[] buffer = new byte[fileStream.Length];
fileStream.Read(buffer, 0, (int)fileStream.Length);
Use This.
return File.ReadAllBytes( shapeFileZip.Name );
Its use For C# code.

Categories